SEsource/三子棋/game.c

117 lines
2.3 KiB
C
Raw Normal View History

2023-08-07 11:00:03 +00:00
#include "game.h"
//<2F><>ʼ<EFBFBD><CABC><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
void initBoard(char board[ROW][COL],int row,int col){
int i = 0;
int j = 0;
for(i = 0;i < row;i++){
for(j = 0;j < col;j++){
board[i][j] = ' ';
}
}
}
//չʾ<D5B9><CABE><EFBFBD><EFBFBD>
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]);
if(j < col -1)
printf("|");
}
printf("\n");
if(i < row -1){
int j = 0;
for(j = 0;j< col;j++){
printf("---");
if(j < col -1)
printf("|");
}
printf("\n");
}
}
}
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
void playMove(char board[][COL],int row,int col){
int x = 0;
int y = 0;
printf("<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>>\n");
while(1){
printf("<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>:>\n");
scanf("%d %d",&x,&y);
//<2F>ж<EFBFBD><D0B6><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϸ<EFBFBD><CFB7><EFBFBD>
if(x >= 1 && x <= row && y>= 1 && y<= col){
//<2F><><EFBFBD><EFBFBD>
//<2F>ж<EFBFBD><D0B6><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ƿ<EFBFBD><C7B7><EFBFBD>ռ<EFBFBD><D5BC>
if(board[x-1][y-1] == ' '){
board[x-1][y-1] = '*';
break;
}else{
printf("<EFBFBD><EFBFBD><EFBFBD>ռ<EFBFBD>ã<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>\n");
}
}else{
printf("<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ƿ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>\n");
}
}
}
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
void computerMove(char board[][COL],int row,int col){
printf("<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>\n");
while(1){
int x = rand() % ROW;
int y = rand() % COL;
//<2F>ж<EFBFBD>λ<EFBFBD><CEBB><EFBFBD>Ƿ<EFBFBD><C7B7><EFBFBD>ռ<EFBFBD><D5BC>
if(board[x][y] == ' '){
board[x][y] = '#';
break;
}
}
}
//<2F>ж<EFBFBD><D0B6><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ƿ<EFBFBD><C7B7><EFBFBD><EFBFBD><EFBFBD>
int isFull(char board[ROW][COL],int row,int col){
int i = 0 ;
int j = 0;
for(i = 0;i < row;i++){
for(j = 0;j< col;j++){
if(board[i][j] == ' '){
//<2F><><EFBFBD><EFBFBD>û<EFBFBD><C3BB>
return 0;
}
}
}
return 1;//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
}
//<2F>ж<EFBFBD><D0B6><EFBFBD>Ϸ<EFBFBD><CFB7><EFBFBD><EFBFBD>
char isWin(char board[ROW][COL],int row,int col){
int i = 0;
//<2F>ж<EFBFBD><D0B6><EFBFBD><EFBFBD><EFBFBD>
for(i = 0;i < row;i++){
if(board[i][0] == board[i][1] && board[i][1] == board[i][2] && board[i][1] != ' '){
return board[i][1];
}
}
//<2F>ж<EFBFBD><D0B6><EFBFBD><EFBFBD><EFBFBD>
for(i = 0;i< col;i++){
if(board[0][i] == board[1][i] && board[1][i] == board[2][i] && board[1][i] != ' '){
return board[1][i];
}
}
//<2F>ж϶Խ<CFB6><D4BD><EFBFBD>
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];
}
//<2F>ж<EFBFBD>ƽ<EFBFBD><C6BD>
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>1<EFBFBD><31><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>0
int ret = isFull(board,row,col);
if(ret == 1){
return 'Q';
}
//<2F><><EFBFBD><EFBFBD>
return 'C';
}