C++实现井字棋游戏
#include<iostream>
#include<cstdlib>
#include<cstdio>
using namespace std;
class TicTacToe
{
private:
char currentPlayer;
char board[3][3];
public:
TicTacToe();
void print();
char getCurrentPlayer();
char getWinner();
bool isDone();
bool isValidMove(int row,int col);
void makeMove(int row,int col);
void makeAutoMove();
};
TicTacToe::TicTacToe(){
currentPlayer = 'X';
for(int i=0;i<3;i++){
for(int j=0;j<3;j++){
board[i][j] = '-';
}
}
}
void TicTacToe::print(){
cout<<" 1 2 3"<<endl;cout<<endl;
for(int i=0;i<3;i++){
cout<<i+1<<" ";
for(int j=0;j<3;j++){
cout<<board[i][j]<<" ";
}
cout<<endl;cout<<endl;
}
}
char TicTacToe::getCurrentPlayer(){
return currentPlayer;
}
char TicTacToe::getWinner(){
//返回赢家信息(’X’ 或 ’O’),如果没有赢家,返回 ’-’
for(int i=0;i<3;i++){
//三棋子横成一排
if(board[i][0] != '-' && board[i][0] == board[i][1] && board[i][1] == board[i][2])
return board[i][0];
//三棋子竖成一排
if(board[0][i] != '-' && board[0][i] == board[1][i] && board[1][i] == board[2][i])
return board[0][i];
}
//三棋子斜成一排
if((board[1][1] != '-') && (
(board[0][0] == board[1][1] && board[1][1] == board[2][2])||
(board[0][2] == board[1][1] && board[1][1] == board[2][0]) )
){
return board[1][1];
}else{
return '-';
}
}
bool TicTacToe::isDone(){
if(getWinner() != '-'){
return true;
}else{
return false;
}
}
bool TicTacToe::isValidMove(int row,int col){
//检查行列值是否在 1-3 之间,如果该位置可用,返回ture,否则返回 false
if( row>=1 && row<=3 && col>=1 && col <=3 ){
return true;
}else{
return false;
}
}
void TicTacToe::makeMove(int row,int col){
//将指定位置设置为玩家的符号,切换玩家
if(isValidMove(row,col)){
if( board[row-1][col-1] == '-' ){
board[row-1][col-1] = currentPlayer;
if(currentPlayer == 'X'){
currentPlayer = 'O';
}else{
currentPlayer = 'X';
}
}else{
cout<<"该位置已被占用!"<<endl;
}
}else{
cout<<"输入的位置无效"<<endl;
}
}
void TicTacToe::makeAutoMove(){
srand(int(time(0)));
label:
int row = rand()%3+1;
int col = rand()%3+1;
if( board[row-1][col-1] == '-' ){
board[row-1][col-1] = currentPlayer;
if(currentPlayer == 'X'){
currentPlayer = 'O';
}else{
currentPlayer = 'X';
}
}else{
goto label;
}
row = rand()%3+1;
col = rand()%3+1;
}
void testTicTacToe(){
cout<<"您是否想开始游戏?(y/n)"<<endl;
char conti;
int mode;
cin>>conti;
TicTacToe tic;
while(conti == 'y'){
cout<<"请选择单人模式或者双人模式?(1/2)"<<endl;
cin>>mode;
if(mode == 1 || mode == 2){
if(mode == 1){
tic.print();
int row,col;
bool flag = true; //表征当前玩家为用户或计算机
while(!tic.isDone()){
if(flag){ //当前玩家为用户
cout<<"当前玩家为:"<<tic.getCurrentPlayer()<<endl;
cout<<"请按坐标输入您的走步:";
cin>>row>>col;
if(tic.getCurrentPlayer() == 'X'){
tic.makeMove(row,col);
}else{
tic.makeAutoMove();
}
flag = false;
}else{ //当前玩家为计算机
cout<<"当前玩家为:"<<tic.getCurrentPlayer()<<endl;
tic.makeAutoMove();
flag = true;
}
tic.print();
}
}else{ //mode == 2
tic.print();
int row,col;
while(!tic.isDone()){
cout<<"当前玩家为:"<<tic.getCurrentPlayer()<<endl;
cout<<"请按坐标输入您的走步:";
cin>>row>>col;
tic.makeMove(row,col);
tic.print();
}
}
cout<<"本局结束,胜者为:"<<tic.getWinner()<<endl;
}else{
cout<<"请输入正确的模式代码"<<endl;
}
cout<<"您是否想继续游戏?(y/n)"<<endl;
cin>>conti;
}
}
int main(){
testTicTacToe();
return 0;
}