c++编写三子棋游戏程序(修改宏定义N,可变成四子棋、五子棋、六子棋....),代码如下:
//c++小练习——编写三子棋游戏程序
#include<iostream>
#include<cstdlib>
#include<time.h>
using namespace std;
#define N 3//棋盘大小定义
void game();
void menu();
void initialize_board(char board[][N]);
void print_board(char board[][N]);
void player_play(char board[][N]);
void computer_play(char board[][N]);
int board_full(char board[][N]);
char game_state(char board[][N]);
int main()
{int input = 0;srand((unsigned int)time(NULL));do//菜单选择{menu();cout << "请选择(1/0): ";cin >> input;switch (input){case 1:cout << "开始游戏" << endl;game();break;case 0:cout << "退出游戏" << endl;break;default:cout << "选择错误,请重新选择" << endl;}} while (input);return 0;
}
void game()//游戏程序
{char board[N][N];initialize_board(board);print_board(board);while (1){player_play(board);print_board(board);if(game_state(board) != 'N')//判断游戏是否继续break;computer_play(board);print_board(board);if(game_state(board) != 'N') break;}switch (game_state(board))//判断游戏结果{case 'X':cout << "玩家获胜" << endl;print_board(board);break;case 'O':cout << "电脑获胜" << endl;print_board(board);break;case 'M':cout << "双方平局" << endl;print_board(board);break;}
}
void menu()//菜单
{cout << "************************" << endl;cout << "******** 三子棋 ********" << endl;cout << "******** 1.play ********" << endl;cout << "******** 0.exit ********" << endl;cout << "************************" << endl;
}
void initialize_board(char board[][N])//初始化棋盘
{int i = 0, j = 0;for (i = 0;i < N;i++)for (j = 0;j < N;j++)board[i][j] = ' ';
}
void print_board(char board[][N])//打印棋盘
{int i = 0, j = 0;for (i = 0; i < N; i++){for (j = 0;j < N;j++)if (j < N - 1)cout <<" "<< board[i][j] << " |";elsecout << board[i][j] << endl;if (i < N - 1){for (j = 0;j < N;j++)if (j < N - 1)cout << "----";elsecout << "---";cout << endl;}}
}
void player_play(char board[][N])//玩家方下棋
{int x = 0, y = 0;cout << "轮到玩家方" << endl;while (1){cout << "请玩家方输入下棋坐标(x y): ";cin >> x >> y;if ((x >= 1 && x <= N) && (y >= 1 && y <= N)){if (board[x - 1][y - 1] == ' '){board[x - 1][y - 1] = 'X';break;}elsecout << "输入坐标已被占用,请重新输入" << endl;}elsecout << "坐标输入错误,请重新输入" << endl;}
}
void computer_play(char board[][N])//电脑方下棋
{cout << "轮到电脑方" << endl;cout << "电脑方正在下棋....." << endl;while (1){int x = rand()%N;int y = rand()%N;if (board[x][y] == ' '){board[x][y] = 'O';break;}}
}
int board_full(char board[][N])//判断棋盘满与否
{int i = 0;int j = 0;for (i = 0; i < N; i++)for (j = 0;j < N;j++)if (board[i][j] == ' ')return 1;//棋盘没满return 0;//棋盘满了
}
char game_state(char board[][N])//游戏状态判断(玩家赢了/电脑赢了/平局/游戏继续)
{int i = 0, j = 0, count = 0;//判断获胜条件:行相等for (i = 0; i < N; i++){count = 0;for (j = 0;j < N;j++){if ((board[i][0] == board[i][j]) && (board[i][0] != ' '))count++;}if (count == N)return board[i][0];}//判断获胜条件:列相等for (i = 0; i < N; i++){count = 0;for (j = 0;j < N;j++){if ((board[0][i] == board[j][i]) && (board[0][i] != ' '))count++;}if (count == N)return board[0][i];}//判断获胜条件:左对角斜相等count = 0;for (i = 0;i < N;i++){if ((board[0][0] == board[i][i])&&(board[0][0]!=' '))count++;}if (count == N)return board[0][0];//判断获胜条件:右对角斜相等count = 0;for (i = 0;i < N;i++){if ((board[0][N - 1] == board[i][N - i - 1])&&(board[0][N-1] != ' '))count++;}if (count == N)return board[0][N-1];if (board_full(board))return 'N';elsereturn 'M';
}//玩家赢了返回X;电脑赢了返回O;平局返回M;游戏继续返回N
程序执行结果: