//函数声明 voidInitBoard(char board[ROW][COL], int row, int col); voidDisplayBoard(char board[ROW][COL], int row, int col); voidPlayerMove(char board[ROW][COL], int row, int col); voidComputerMove(char board[ROW][COL], int row, int col); charWin(char board[ROW][COL], int row, int col); charFull(char board[ROW][COL], int row, int col);
//函数声明 voidInitBoard(char board[ROW][COL], int row, int col); voidDisplayBoard(char board[ROW][COL], int row, int col); voidPlayerMove(char board[ROW][COL], int row, int col); voidComputerMove(char board[ROW][COL], int row, int col); charWin(char board[ROW][COL], int row, int col); charFull(char board[ROW][COL], int row, int col);
charWin(char board[ROW][COL], int row, int col)//五子棋 { int x = 0; int y = 0; int i = 0; for (x = 0; x < row; x++) { for (y = 0; y < col; y++) { //纵向 for (i = 0; i < CHESS - 1; i++) { if (board[x][y] == board[x][y + i] && board[x][y] != ' ') { continue; } else break; } if (board[x][y] == board[x][y + i] && board[x][y] != ' ') { return board[x][y]; }
//横向 for (i = 0; i < CHESS - 1; i++) { if (board[x][y] == board[x + i][y] && board[x][y] != ' ') { continue; } else break; } if (board[x][y] == board[x + i][y] && board[x][y] != ' ') { return board[x][y]; }
//捺 斜向 for (i = 0; i < CHESS - 1; i++) { if (board[x][y] == board[x + i][y + i] && board[x][y] != ' ') { continue; } else break; } if (board[x][y] == board[x + i][y + i] && board[x][y] != ' ') { return board[x][y]; }
//撇 斜向 for (i = 0; i < CHESS - 1; i++) { if (board[x][y] == board[x - i][y + i] && board[x][y] != ' ') { continue; } else break; } if (board[x][y] == board[x - i][y + i] && board[x][y] != ' ') { return board[x][y]; } } } if (Full(board, ROW, COL)) { return'Q'; } return'C'; }
//打印棋盘(3*3-9*9) voidDisplayBoard(char board[ROW][COL], int row, int col) { for (int i = 0; i < col; i++) //新增 { if (i == 0) //新增 { printf(" %d ", i + 1);//新增 } else//新增 { printf("%d ", i + 1); } } printf("\n"); //新增,棋盘网格上加入相应列数
for ( int i = 0; i < row; i++) { for (int j = 0; j < col; j++) { if (j == 0) //新增 { printf("%d %c |",i+1,board[i][j]); //新增,在第一列之前打印出相应行数。 } elseif (j>0 && j < col - 1) //改动 { printf(" %c |", board[i][j]); } else printf(" %c ", board[i][j]); } printf("\n"); if (i < row - 1) {
for (int k = 0; k < col; k++) { if (k == 0) { printf(" ---|");//改动改动改动了前面加空格以适应上面的变化 } elseif (k>0 && k < col - 1) { printf("---|"); } else { printf("---"); } } } printf("\n"); } }
//打印棋盘(10*10-99*99) voidDisplayBoard(char board[ROW][COL], int row, int col) { for (int i = 0; i < col; i++) { if (i == 0) { printf(" %02d ", i + 1); } else { printf("%02d ", i + 1); } } printf("\n");
for (int i = 0; i < row; i++) { for (int j = 0; j < col; j++) { if (j == 0) { printf("%02d %c |", i + 1, board[i][j]);//这里修改了下,自动补0, } elseif (j > 0 && j < col - 1) { printf(" %c |", board[i][j]); } else printf(" %c ", board[i][j]); } printf("\n"); if (i < row - 1) {
for (int k = 0; k < col; k++) { if (k == 0) { printf(" ---|"); } elseif (k > 0 && k < col - 1) { printf("---|"); } else { printf("---"); } } } printf("\n"); } }
完整代码
第一类五子棋完整代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
//宏定义 #define _CRT_SECURE_NO_WARNINGS 1 #define ROW 5 #define COL 5
//函数声明 voidInitBoard(char board[ROW][COL], int row, int col); voidDisplayBoard(char board[ROW][COL], int row, int col); voidPlayerMove(char board[ROW][COL], int row, int col); voidComputerMove(char board[ROW][COL], int row, int col); charWin(char board[ROW][COL], int row, int col); charFull(char board[ROW][COL], int row, int col);
//初始化棋盘 voidInitBoard(char board[ROW][COL], int row, int col) { for (int i = 0; i < row; i++) { for (int j = 0; j < col; j++) { board[i][j] = ' '; } } }
//打印棋盘 voidDisplayBoard(char board[ROW][COL], int row, int col) { for (int i = 0; i < col; i++) { if (i == 0) { printf(" %d ", i + 1); } else { printf("%d ", i + 1); } } printf("\n");
for (int i = 0; i < row; i++) { for (int j = 0; j < col; j++) { if (j == 0) { printf("%d %c |", i + 1, board[i][j]); } elseif (j > 0 && j < col - 1) { printf(" %c |", board[i][j]); } else printf(" %c ", board[i][j]); } printf("\n"); if (i < row - 1) {
for (int k = 0; k < col; k++) { if (k == 0) { printf(" ---|"); } elseif (k > 0 && k < col - 1) { printf("---|"); } else { printf("---"); } } } printf("\n"); } }
//玩家输入 voidPlayerMove(char board[ROW][COL], int row, int col) { int x = 0; int y = 0; while (1) { printf("系统提示:请输入您要下的坐标:>>>");
scanf("%d%d", &x, &y); printf("\n"); //判断xy是否合法,一个是是否溢出,一个是是否被占用 if (x > 0 && x < row + 1 && y>0 && y < col + 1) { if (board[x - 1][y - 1] == ' ') { board[x - 1][y - 1] = '*'; break; } else { printf("电脑:这下过了,你个憨憨。\n"); printf("\n");
//电脑输入 voidComputerMove(char board[ROW][COL], int row, int col) { int x = 0; int y = 0; printf("电脑:我走过啦!!!到你啦!!!\n\n"); while (1) { x = rand() % row; y = rand() % col; if (board[x][y] == ' ') { board[x][y] = '#'; break; } } }
//判断胜负 charWin(char board[ROW][COL], int row, int col) { int i = 0; //判断行 for (i = 0; i < row; i++) { if (board[i][0] == board[i][1] && board[i][1] == board[i][2] && board[i][2] == board[i][3] && board[i][3] == board[i][4] && board[i][1] != ' ')//行元素一样且不是空格 { return board[i][1]; } } //判断列 for (i = 0; i < col; i++) { if (board[0][i] == board[1][i] && board[1][i] == board[2][i] && board[2][i] == board[3][i] && board[3][i] == board[4][i] && board[1][i] != ' ')//列元素一样且不是空格 { return board[1][i]; } } //判断对角线 if (board[0][0] == board[1][1] && board[1][1] == board[2][2] && board[2][2] == board[3][3] && board[3][3] == board[4][4] && board[1][1] != ' ')//对角线元素一样且不是空格 { return board[1][1]; } if (board[4][0] == board[3][1] && board[3][1] == board[2][2] && board[2][2] == board[1][3] && board[1][3] == board[0][4] && board[2][2] != ' ') { return board[1][1]; } //判断是否平局 if (1 == Full(board, ROW, COL))//平局判断,函数在下面,已在头文件中声明,位置就无所谓了 { return'Q'; } return'C'; }
//平局算法函数 charFull(char board[ROW][COL], int row, int col) { for (int i = 0; i < row; i++) { for (int j = 0; j < col; j++) { if (board[i][j] == ' ') return0; } } return1; }
//函数声明 voidInitBoard(char board[ROW][COL], int row, int col); voidDisplayBoard(char board[ROW][COL], int row, int col); voidPlayerMove(char board[ROW][COL], int row, int col); voidComputerMove(char board[ROW][COL], int row, int col); charWin(char board[ROW][COL], int row, int col); charFull(char board[ROW][COL], int row, int col);
//初始化棋盘 voidInitBoard(char board[ROW][COL], int row, int col) { for (int i = 0; i < row; i++) { for (int j = 0; j < col; j++) { board[i][j] = ' '; } } }
//打印棋盘 voidDisplayBoard(char board[ROW][COL], int row, int col) { for (int i = 0; i < col; i++) { if (i == 0) { printf(" %02d ", i + 1); } else { printf("%02d ", i + 1); } } printf("\n");
for (int i = 0; i < row; i++) { for (int j = 0; j < col; j++) { if (j == 0) { printf("%02d %c |", i + 1, board[i][j]); } elseif (j > 0 && j < col - 1) { printf(" %c |", board[i][j]); } else printf(" %c ", board[i][j]); } printf("\n"); if (i < row - 1) {
for (int k = 0; k < col; k++) { if (k == 0) { printf(" ---|"); } elseif (k > 0 && k < col - 1) { printf("---|"); } else { printf("---"); } } } printf("\n"); } }
//玩家输入 voidPlayerMove(char board[ROW][COL], int row, int col) { int x = 0; int y = 0; while (1) { printf("系统提示:请输入您要下的坐标:>>>");
scanf("%d%d", &x, &y); printf("\n"); //判断xy是否合法,一个是是否溢出,一个是是否被占用 if (x > 0 && x < row + 1 && y>0 && y < col + 1) { if (board[x - 1][y - 1] == ' ') { board[x - 1][y - 1] = '*'; break; } else { printf("电脑:这下过了,你个憨憨。\n"); printf("\n");
//电脑输入 voidComputerMove(char board[ROW][COL], int row, int col) { int x = 0; int y = 0; printf("电脑:我走过啦!!!到你啦!!!\n\n"); while (1) { x = rand() % row; y = rand() % col; if (board[x][y] == ' ') { board[x][y] = '#'; break; } } }
charWin(char board[ROW][COL], int row, int col)//五子棋 { int x = 0; int y = 0; int i = 0; for (x = 0; x < row; x++) { for (y = 0; y < col; y++) { //纵向 for (i = 0; i < CHESS - 1; i++) { if (board[x][y] == board[x][y + i] && board[x][y] != ' ') { continue; } else break; } if (board[x][y] == board[x][y + i] && board[x][y] != ' ') { return board[x][y]; }
//横向 for (i = 0; i < CHESS - 1; i++) { if (board[x][y] == board[x + i][y] && board[x][y] != ' ') { continue; } else break; } if (board[x][y] == board[x + i][y] && board[x][y] != ' ') { return board[x][y]; }
//捺 斜向 for (i = 0; i < CHESS - 1; i++) { if (board[x][y] == board[x + i][y + i] && board[x][y] != ' ') { continue; } else break; } if (board[x][y] == board[x + i][y + i] && board[x][y] != ' ') { return board[x][y]; }