目录(标2的是我自己写的一堆问题不知道怎么改)
- 开始菜单1
- 打印棋盘1
- 玩家下棋1
- 电脑下棋1
- 判断输赢1
- 开始菜单2
- 打印棋盘2
- 选择先后2
- 玩家下棋2
- 电脑下棋2
- 判断输赢2
- 完整代码
- 文件else.h
- 文件else.c
- 文件test.c
开始菜单1
void menu()//打印菜单
{printf("******************\n");printf("******1.play******\n");printf("******0.exit******\n");printf("******************\n");
}
打印棋盘1
//存放下棋数据
char board[ROW][COL] = { 0 };
//初始化棋盘为全空格
InitBoard(board, ROW, COL);
//初始化棋盘为空格
void InitBoard(char 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++){board[i][j] = ' ';}}
}
//打印棋盘
void DisplayBoard(char 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++){printf(" %c ",board[i][j]);//别忘记board[i][j]if (j < col - 1){printf("|");}}printf("\n");//打印分割行if (i < row-1)//记住 row-1{for (j = 0; j < col; j++){printf("---");if (j < col - 1){printf("|");}}printf("\n");}}
}
玩家下棋1
//玩家下棋
void player_move(char board[ROW][COL], int row, int col)
{int x = 0;int y = 0;printf("玩家下棋\n");while (1){printf("请输入坐标:");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");}}
电脑下棋1
void computer_move(char board[ROW][COL], int row, int col)
{int x = 0;int y = 0;printf("电脑下棋:>\n");while (1){x = rand() % 3;//范围(a,b)用rand表示就是rand()%(b-a+1)y = rand() % 3;if (board[x][y] == ' ')//判断是否为空格,因为只有这一个条件,所以满足这个条件就可以下{board[x][y] = '#';break;}}
}
判断输赢1
while (1)//玩家和电脑轮流下棋,直到一方胜利{ //玩家下棋player_move(board, ROW, COL);//打印棋盘DisplayBoard(board, ROW, COL);ret = is_win(board, ROW, COL);if (ret != 'C'){break;}//电脑下棋computer_move(board, ROW, COL);//打印棋盘DisplayBoard(board, ROW, COL);ret = is_win(board, ROW, COL);if (ret != 'C'){break;}}if (ret == '*'){printf("玩家获胜\n");}else if (ret == '#'){printf("电脑获胜\n");}else // (ret == 'Q'){printf("平局\n");}DisplayBoard(board, ROW, COL);
}//判断输赢
char is_win(char board[ROW][COL], int row, int col)
{//判断每一行是否都相等int i = 0;for (i = 0; i < col; i++){if (board[i][0] == board[i][1] && board[i][1] == board[i][2] && 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[1][i] != ' '){return board[i][1];}}//判断对角线是否都相等if (board[0][0] == board[1][1] && board[1][1] == board[2][2] && board[1][1] != ' '){return board[1][1];}if (board[0][2] == board[1][1] && board[1][1] == board[2][0] && board[1][1] != ' '){return board[1][1];}//判断平局if (is_full(board, row, col) == 1){return 'Q';}//继续return 'C';
}
开始菜单2
int begin()
{printf("##########请选择##########\n");printf("##########1:play##########\n");printf("##########0:exit##########\n");printf("##########################\n");int a = 0;scanf("%d", &a);if (a == 0 || a == 1)return 1;else{printf("输入错误,请重新输入\n");return 0;}
}
打印棋盘2
void play(char arr[3][3])
{for (int i = 0; i < 3; i++){for (int j = 0; j < 3; j++){if (j < 2)printf(" %c |", arr[i][j]);elseprintf(" % c ", arr[i][j]);}printf("\n");printf("___|___|___\n");}
}
选择先后2
int select()
{int select1;printf("##########################\n");printf("######请选择先后顺序######\n");printf("#########1:玩家先#########\n");printf("#########2:电脑先#########\n");printf("##########################\n");scanf("%d", &select1);if(select1==1||select1==2)return select1;else{printf("输入错误,请重新输入\n");return 0;}
}
玩家下棋2
int player(char arr[3][3])
{printf("请输入坐标\n");int x , y ;scanf("%d %d", &x, &y);if (x >= 1 && x <= 3 && y >= 1 && y <= 3 && arr[x-1][y-1] != '@'){arr[y - 1][x - 1] = '*';return 1;}else{printf("输入错误,请重新输入\n");return 0;}
}
电脑下棋2
void computer(char arr[3][3])
{while (1){srand((unsigned int)time(NULL));int r1 = 0 + rand() % (2 - 0 + 1);int r2 = 0 + rand() % (2 - 0 + 1);if (arr[r1][r2] == '#'){arr[r1][r2] = '@';break;}}
}
判断输赢2
这里不想思考了所以直接就把这些情况都列举出来了
int over(char arr[3][3])
{if (arr[0][0] =='*' && arr[0][0] == arr[1][0] && arr[1][0] == arr[2][0])//横着的第一行为*{printf("玩家获胜\n");return 1;}else if (arr[1][0] == '*' && arr[1][0] == arr[1][1] && arr[1][1] == arr[1][2])//横着的第二行为*{printf("玩家获胜\n");return 1;}else if (arr[2][0] == '*' && arr[2][0] == arr[2][1] && arr[2][1] == arr[2][2])//横着的第三行为*{printf("玩家获胜\n");return 1;}else if (arr[0][0] == '*' && arr[0][0] == arr[0][1] && arr[0][1] == arr[0][2])//竖着的第一列为*{printf("玩家获胜\n");return 1;}else if (arr[1][0] == '*' && arr[1][0] == arr[1][1] && arr[1][1] == arr[1][2])//竖着的第二列为*{printf("玩家获胜\n");return 1;}else if (arr[2][0] == '*' && arr[2][0] == arr[2][1] && arr[2][1] == arr[2][2])//竖着的第三列为*{printf("玩家获胜\n");return 1;}else if (arr[0][0] == '*' && arr[0][0] == arr[1][1] && arr[1][1] == arr[2][2])//对角线左到右为*{printf("玩家获胜\n");return 1;}else if (arr[2][0] = '*' && arr[2][0] == arr[1][1] && arr[1][1] == arr[0][2])//对角线右到左为*{printf("玩家获胜\n");return 1;}else if (arr[0][0] == '@' && arr[0][0] == arr[1][0] && arr[1][0] == arr[2][0])//横着的第一行为*{printf("电脑获胜\n");return 1;}else if (arr[1][0] == '@' && arr[1][0] == arr[1][1] && arr[1][1] == arr[1][2])//横着的第二行为*{printf("电脑获胜\n");return 1;}else if (arr[2][0] == '@' && arr[2][0] == arr[2][1] && arr[2][1] == arr[2][2])//横着的第三行为*{printf("电脑获胜\n");return 1;}else if (arr[0][0] == '@' && arr[0][0] == arr[0][1] && arr[0][1] == arr[0][2])//竖着的第一列为*{printf("电脑获胜\n");return 1;}else if (arr[1][0] == '@' && arr[1][0] == arr[1][1] && arr[1][1] == arr[1][2])//竖着的第二列为*{printf("电脑获胜\n");return 1;}else if (arr[2][0] =='@' && arr[2][0] == arr[2][1] && arr[2][1] == arr[2][2])//竖着的第三列为*{printf("电脑获胜\n");return 1;}else if (arr[0][0] =='@' && arr[0][0] == arr[1][1] && arr[1][1] == arr[2][2])//对角线左到右为*{printf("电脑获胜\n");return 1;}else if (arr[2][0] == '@' && arr[2][0] == arr[1][1] && arr[1][1] == arr[0][2])//对角线右到左为*{printf("电脑获胜\n");return 1;}else{int count = 0;for (int i = 0; i < 3; i++){for (int j = 0; j < 3; j++){if (arr[i][j] != '#')count++;}}if (count == 9){printf("平局\n");return 2;}}
}
完整代码
文件else.h
#pragma once
int begin();
void play(char arr[3][3]);
int select();
void computer(char arr[3][3]);
int player(char arr[3][3]);
int over(char arr[3][3]);
文件else.c
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
int begin()
{printf("##########请选择##########\n");printf("##########1:play##########\n");printf("##########0:exit##########\n");printf("##########################\n");int a = 0;scanf("%d", &a);if (a == 0 || a == 1)return 1;else{printf("输入错误,请重新输入\n");return 0;}
}
void play(char arr[3][3])
{for (int i = 0; i < 3; i++){for (int j = 0; j < 3; j++){if (j < 2)printf(" %c |", arr[i][j]);elseprintf(" % c ", arr[i][j]);}printf("\n");printf("___|___|___\n");}
}
int select()
{int select1;printf("##########################\n");printf("######请选择先后顺序######\n");printf("#########1:玩家先#########\n");printf("#########2:电脑先#########\n");printf("##########################\n");scanf("%d", &select1);if(select1==1||select1==2)return select1;else{printf("输入错误,请重新输入\n");return 0;}
}
void computer(char arr[3][3])
{while (1){srand((unsigned int)time(NULL));int r1 = 0 + rand() % (2 - 0 + 1);int r2 = 0 + rand() % (2 - 0 + 1);if (arr[r1][r2] == '#'){arr[r1][r2] = '@';break;}}
}int player(char arr[3][3])
{printf("请输入坐标\n");int x , y ;scanf("%d %d", &x, &y);if (x >= 1 && x <= 3 && y >= 1 && y <= 3 && arr[x-1][y-1] != '@'){arr[y - 1][x - 1] = '*';return 1;}else{printf("输入错误,请重新输入\n");return 0;}
}int over(char arr[3][3])
{if (arr[0][0] =='*' && arr[0][0] == arr[1][0] && arr[1][0] == arr[2][0])//横着的第一行为*{printf("玩家获胜\n");return 1;}else if (arr[1][0] == '*' && arr[1][0] == arr[1][1] && arr[1][1] == arr[1][2])//横着的第二行为*{printf("玩家获胜\n");return 1;}else if (arr[2][0] == '*' && arr[2][0] == arr[2][1] && arr[2][1] == arr[2][2])//横着的第三行为*{printf("玩家获胜\n");return 1;}else if (arr[0][0] == '*' && arr[0][0] == arr[0][1] && arr[0][1] == arr[0][2])//竖着的第一列为*{printf("玩家获胜\n");return 1;}else if (arr[1][0] == '*' && arr[1][0] == arr[1][1] && arr[1][1] == arr[1][2])//竖着的第二列为*{printf("玩家获胜\n");return 1;}else if (arr[2][0] == '*' && arr[2][0] == arr[2][1] && arr[2][1] == arr[2][2])//竖着的第三列为*{printf("玩家获胜\n");return 1;}else if (arr[0][0] == '*' && arr[0][0] == arr[1][1] && arr[1][1] == arr[2][2])//对角线左到右为*{printf("玩家获胜\n");return 1;}else if (arr[2][0] = '*' && arr[2][0] == arr[1][1] && arr[1][1] == arr[0][2])//对角线右到左为*{printf("玩家获胜\n");return 1;}else if (arr[0][0] == '@' && arr[0][0] == arr[1][0] && arr[1][0] == arr[2][0])//横着的第一行为*{printf("电脑获胜\n");return 1;}else if (arr[1][0] == '@' && arr[1][0] == arr[1][1] && arr[1][1] == arr[1][2])//横着的第二行为*{printf("电脑获胜\n");return 1;}else if (arr[2][0] == '@' && arr[2][0] == arr[2][1] && arr[2][1] == arr[2][2])//横着的第三行为*{printf("电脑获胜\n");return 1;}else if (arr[0][0] == '@' && arr[0][0] == arr[0][1] && arr[0][1] == arr[0][2])//竖着的第一列为*{printf("电脑获胜\n");return 1;}else if (arr[1][0] == '@' && arr[1][0] == arr[1][1] && arr[1][1] == arr[1][2])//竖着的第二列为*{printf("电脑获胜\n");return 1;}else if (arr[2][0] =='@' && arr[2][0] == arr[2][1] && arr[2][1] == arr[2][2])//竖着的第三列为*{printf("电脑获胜\n");return 1;}else if (arr[0][0] =='@' && arr[0][0] == arr[1][1] && arr[1][1] == arr[2][2])//对角线左到右为*{printf("电脑获胜\n");return 1;}else if (arr[2][0] == '@' && arr[2][0] == arr[1][1] && arr[1][1] == arr[0][2])//对角线右到左为*{printf("电脑获胜\n");return 1;}else{int count = 0;for (int i = 0; i < 3; i++){for (int j = 0; j < 3; j++){if (arr[i][j] != '#')count++;}}if (count == 9){printf("平局\n");return 2;}}
}
文件test.c
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include"else.h"
int main()
{int r = 0;char arr[3][3] = { {'#','#','#'},{'#','#','#'},{'#','#','#' } };while (1){if (begin() == 1)break;}play(arr);while (1){r = select();if(r==1||r==2)break;}while (1){if (r == 1){while (1){while (1){if (player(arr) == 0)break;play(arr);printf("\n");computer(arr);play(arr);printf("\n");over(arr);}}}else{while (1){computer(arr);play(arr);printf("\n");player(arr); play(arr);printf("\n");over(arr);}}}return 0;
}