井子棋 类
chess.h文件
#ifndef CHESS_H_INCLUDED
#define CHESS_H_INCLUDED
#include<iostream>
#include<stdio.h>
#include<string>
#include<vector>
using namespace std;
const char black='X';
const char white='O';
const char blank=' ';
const int number=9;
const int chess_row=5;
const int chess_col=5;
class chess{
public:
chess();
bool isEmpty(int pos);
bool isFull();
bool isWinner(char c);
void placeBlack(int pos);
void placeWhite(int pos);
void printChess();
bool canWin();
private:
vector<char>boardOneLine;
vector<string>board;
};
#endif // CHESS_H_INCLUDEDchess.cpp文件
#include<iostream>
#include<string>
#include<stdio.h>
#include<stdlib.h>
#include"chess.h"
using namespace std;
chess::chess(){
vector<string>tmp;
tmp.push_back(" | | ");
tmp.push_back("- - -");
tmp.push_back(" | | ");
tmp.push_back("- - -");
tmp.push_back(" | | ");
board=tmp;
boardOneLine.resize(number);
for(int i=0;i<number;i++)
boardOneLine[i]=' ';
}
bool chess::isEmpty(int pos){
return boardOneLine[pos]==' ';
}
bool chess::isFull(){
for(int i=0;i<number;i++)
if(boardOneLine[i]==' ')
return false;
return true;
}
bool chess::isWinner(char c){
//for(int i=0;i<9;i++)cout<<boardOneLine[i];
//cout<<endl;
for(int i=0;i<=2;i++){
if(boardOneLine[i]==c&&boardOneLine[i+3]==c&&boardOneLine[i+6]==c)
return true;
}
for(int i=0;i<=6;i+=3){
if(boardOneLine[i]==c&&boardOneLine[i+1]==c&&boardOneLine[i+2]==c)
return true;
}
if(boardOneLine[0]==c&&boardOneLine[4]==c&&boardOneLine[8]==c)
return true;
if(boardOneLine[2]==c&&boardOneLine[4]==c&&boardOneLine[6]==c)
return true;
return false;
}
void chess::placeWhite(int pos){
boardOneLine[pos]='O';
board[(pos/3)*2][(pos%3)*2]='O';
}
void chess::placeBlack(int pos){
boardOneLine[pos]='X';
board[(pos/3)*2][(pos%3)*2]='X';
}
void chess::printChess(){
int i;
cout<<"----------------------------------"<<endl;
cout<<"此时局势:"<<endl;
for(i=0;i<chess_row;i++){
cout<<board[i]<<endl;
}
}对局类
tic_tac_toe.h
#ifndef TIC_TAC_TOE_H_INCLUDED
#define TIC_TAC_TOE_H_INCLUDED
#include<iostream>
#include<stdio.h>
#include<string>
#include"chess.h"
enum Role{BlackRole,WhiteRole};
class tic_tac_toe{
public:
void start();
Role chooseRole();
void BlackPlace();
void WhitePlace();
bool gameOver();
private:
chess board;
};
#endif // TIC_TAC_TOE_H_INCLUDED
tic_tac_toe.cpp
#include<iostream>
#include<stdio.h>
#include<random>
#include<stdlib.h>
#include<time.h>
#include"tic_tac_toe.h"
void tic_tac_toe::start(){
Role first=chooseRole();
if(first==BlackRole){
BlackPlace();
}
while(1){
board.printChess();
if(gameOver())break;
WhitePlace();
board.printChess();
if(gameOver())break;
BlackPlace();
}
}
Role tic_tac_toe::chooseRole(){
int tmp;
cout<<"0:黑棋(“X”)"<<endl;
cout<<"1:白棋(“O”)"<<endl;
while(1){
cout<<"----------------------------------"<<endl;
cout<<"请选择先行方:"<<endl;
cin>>tmp;
if(tmp==0||tmp==1){
cout<<endl;
break;
}
else
cout<<"输入错误"<<endl;
}
return (Role)tmp;
}
void tic_tac_toe::WhitePlace(){
int pos;
int i,j;
cout<<""<<endl;
while(1){
cout<<"----------------------------------"<<endl;
cout<<"请输入你放置的棋子的位置:"<<endl;
cout<<"行:"<<endl;
cin>>i;
if(i<0||i>3){
cout<<"输入错误"<<endl;
continue;
}
cout<<"列:"<<endl;
cin>>j;
if(j<0||j>3){
cout<<"输入错误"<<endl;
continue;
}
pos=3*(i-1)+j-1;
if(board.isEmpty(pos))
break;
else{
cout<<"输入错误"<<endl;
continue;
}
}
board.placeWhite(pos);
}
void tic_tac_toe::BlackPlace(){
srand(time(NULL));
while(1){
int pos=rand()%9;
if(board.isEmpty(pos)){
board.placeBlack(pos);
break;
}
}
}
bool tic_tac_toe::gameOver(){
if(board.isWinner('X')){
cout<<"----------------------------------"<<endl;
cout<<"YOU LOSE!"<<endl;
return true;
}
if(board.isWinner('O')){
cout<<"----------------------------------"<<endl;
cout<<"YOU WIN"<<endl;
return true;
}
if(board.isFull()){
cout<<"----------------------------------"<<endl;
cout<<"Draw!"<<endl;
return true;
}
return false;
}
main.cpp
#include"tic_tac_toe.h"
#include"chess.h"
#include<iostream>
using namespace std;
int main(){
while(1){
char c;
tic_tac_toe game;
game.start();
cout<<"重新开始游戏?(Y/N)"<<endl;
cin>>c;
if(c=='N')
break;
}
return 0;
}
本文介绍了一个简单的井字棋游戏程序实现,使用C++语言,包括游戏规则、棋盘管理和胜负判断等功能。玩家可以选择扮演黑白棋中的一种,与电脑进行对弈。
188

被折叠的 条评论
为什么被折叠?



