#include <iostream> #include <stdlib.h> #include <time.h> using namespace std; #define ROW 3 #define COL 3 void game(); int IsFull(int board[ROW][COL], int row, int col); void Init(int board[ROW][COL], int row, int col); void Display(int board[ROW][COL],int row, int col); void Player(int board[ROW][COL], int row, int col); void Computer(int board[ROW][COL], int row, int col); char IsWin(int board[ROW][COL], int row, int col); void menu(); int main() { menu(); int input = 0; do { scanf("%d", &input); switch (input) { case 1: printf("begin game!\n"); game(); break; case 0: break; default: printf("wrong,please reput!\n"); break; } } while (input); return 0; } void menu() { printf("***************************\n"); printf("******** 1.play ********\n"); printf("******** 0.exit ********\n"); printf("***************************\n"); } #include "game.h" void game() { int board[ROW][COL]; int row = ROW; int col = COL; srand((unsigned int)time(NULL)); char ret = 0; Init(board, row, col); while (1) { //player Player(board, row, col); Display(board, row, col); ret = IsWin(board, row, col); if (ret != 'C') break; //Computer printf("电脑落子:->\n"); Computer(board, row, col); Display(board, row, col); ret = IsWin(board, row, col); if (ret != 'C') break; } if (ret == '@') { printf("WINNER:Player!\n"); } else if (ret == '$') { printf("WINNER:Computer!\n"); } else { printf("ping---\n"); } } void Init(int board[ROW][COL],int row,int col) { for (int i = 0; i < row; i++) { for (int j = 0; j < col; j++) { board[i][j] = ' '; } } } void Player(int board[ROW][COL], int row, int col) { int x = 0; int y = 0; while (1) { printf("玩家落子:->\n"); scanf("%d %d", &x, &y); if (x >= 1 && x <= row && y >= 1 && y <= col) { if (board[x - 1][y - 1] == ' ') { board[x - 1][y - 1] = '@'; break; } else { printf("这里下过了,换个地方下吧!\n"); } } else { printf("输入非法,重新输入!\n"); } } } void Computer(int board[ROW][COL], int row, int col) { while (1) { int x = rand() % ROW; int y = rand() % COL; if (board[x][y] == ' ') { board[x][y] = '$'; break; } } } int IsFull(int board[ROW][COL], int row, int col) { int i = 0; for (i = 0; i < row; i++) { int j = 0; for (j = 0; j < col; j++) { if (board[i][j] == ' ') { return 0; } } } return 1; } char IsWin(int board[ROW][COL], int row, int col) { //ROW int flag = 1; int i = 0; for (i = 0; i < row; i++) { for (int j = 0; j < col - 1; j++) { if (board[i][j] == board[i][j + 1] && board[i][j] != ' ') { ; } else { flag = 0; } } if (flag == 1) { return board[i][0]; } flag = 1; } //COL flag = 1; for (i = 0; i < row; i++) { for (int j = 0; j < col - 1; j++) { if (board[j][i] == board[j+1][i] && board[j][i] != ' ') { ; } else { flag = 0; } } if (flag == 1) { return board[0][i]; } flag = 1; } //对角线 flag = 1; for (int k = 0; k < ROW - 1; k++) { if (board[k][k] == board[k + 1][k + 1] && board[1][1] != ' ') ; else flag = 0; } if (flag == 1) { return board[row/2][col/2]; } //col flag = 1; int k, t; for( k = 0,t = col - 1; k < col - 1; k++, t--) { if (board[k][t] == board[k + 1][t - 1] && board[0][col - 1] != ' ') ; else flag = 0; } if (flag == 1) { return board[0][COL-1]; } //Ping if (IsFull(board, row, col) == 1) { return 'Q'; } //继续 return 'C'; } void Display(int board[ROW][COL], int row, int col) { for (int i = 0; i < row; i++) { for (int j = 0; j < col; j++) { printf(" %c ", board[i][j]); if (j < row - 1) printf("|"); } printf("\n"); for (int i = 0; i < row; i++) { printf("---"); if (i < row - 1) printf("|"); } printf("\n"); } }
就是这样子滴,棋盘可以修改大小,但是越大,电脑越智障~