#include "add.h"
void game()
{
char ret=0;
char board[ROW][COL];
inti_board(board,ROW,COL);
print_board(board, ROW, COL);
while(1)
{
player_move(board, ROW, COL);//玩家下棋
print_board(board, ROW, COL);//打印棋盤
ret = is_win(board, ROW, COL);//判斷輸贏
if (ret != 'c')
{
break;
}
computer_move(board, ROW, COL);
print_board(board, ROW, COL);
ret = is_win(board, ROW, COL);
if (ret != 'c')
{
break;
}
}
if (ret == '#')
printf("電腦贏了\n");
else if (ret == '*')
printf("玩家贏了\n");
else if (ret == 'Q')
printf("平局\n");
}
void menu()
{
printf("****************\n");
printf("** 1.play ***\n");
printf("** 0.exit ***\n");
printf("****************\n");
}
void test()
{
srand((unsigned)time(NULL));
int a = 0;
do
{
menu();
printf("請選擇\n");
scanf("%d", &a);
switch (a)
{
case 1: printf("三子棋\n");
game();
break;
case 2: printf("退出游戲\n");
break;
default:printf("重新選擇\n");
}
} while (a);
}
int main()
{
int a = 0;
test();
return 0;
}
2.#include "add.h";
void inti_board(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]=' ';
}
}
}
void print_board(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++)
{
printf(" %c ", board[i][j]);
if (j< col-1)
printf("|");
}
printf("\n");
if (i< row - 1)
for (j = 0; j< col; j++)
{
printf("---");
if (j< col-1)
printf("|");
}
printf("\n");
}
}
void player_move(char board[ROW][COL], int row, int col)
{
printf("玩家下棋\n");
while (1)
{
printf("請輸入坐標(biāo)\n");
int x, y;
scanf("%d %d", &x, &y);
if (x - 1 >= 0 && x - 1< row && y - 1< col && y - 1 >= 0)
{
if (board[x - 1][y - 1] == ' ')
{
board[x - 1][y - 1] = '*';
break;
}
else
printf("該坐標(biāo)已被占用,請重新輸入\n");
}
else
printf("非法坐標(biāo)重新輸入\n");
}
}
void computer_move(char board[ROW][COL], int row, int col)
{
printf("電腦輸入\n");
while (1)
{
int x = rand() % row;
int y = rand() % col;
if (board[x ][y ] == ' ')
{
board[x][y] = '#';
break;
}
}
}
int is_full(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++)
{
if (board[i][j] == ' ')
return 0;
}
}
return 1;
}
char is_win(char board[ROW][COL], int row, int col)
{
int flag = 0;
int i = 0;
int j = 0;
for (i = 0; i< row; i++)
{
flag = 0;
for (j = 1; j< col; j++)
{
if (board[i][j - 1] == board[i][j] && board[i][j - 1] != ' ' && board[i][j] != ' ')
{
flag++ ;
}
}
if (flag == col - 1)
return board[i][j - 1];
}
flag = 0;
for (j = 0; j< col; j++)
{
flag = 0;
for ( i= 1; i< row; i++)
{
if (board[i - 1][j] == board[i][j] && board[i - 1][j] != ' ' && board[i - 1][j] != ' ')
flag ++;
}
if (flag == row - 1)
return board[i-1][j];
}
flag = 0;
for (i = 0; i< row-1; i++)
{
if (board[i][i] != board[i + 1][i + 1] || board[i][i] == ' ' || board[i + 1][i + 1] == ' ')
{
flag = 1;
}
}
if (flag == 0)
return board [i-1][i-1];
flag = 0;
for (i = 1; i< row; i++)
{
if (board[i-1][row - 1-(i-1) ] != board[i ][row-i-1] || board[i-1][row - 1 -(i-1)] == ' ' || board[i] [row-i-1]== ' ')
flag = 1;
}
if(flag==0)
return board[row-1][0];
if (is_full(board, ROW, COL) == 1)
{
return 'Q';
}
return 'c';
}
3.#define _CRT_SECURE_NO_WARNINGS
#define ROW 3
#define COL 3
#include#include#includevoid inti_board(char board[ROW][COL], int row, int col);//小寫因?yàn)椴慌cROW COL沖突
void print_board(char board[ROW][COL],int row,int col);
void player_move(char board[ROW][COL], int row, int col);
void computer_move(char board[ROW][COL], int row, int col);
char is_win(char board[ROW][COL], int row, int col);
二、三子棋游戲函數(shù)(已優(yōu)化)
1.test()#define _CRT_SECURE_NO_WARNINGS
#define ROW 3//N子棋
#define COL 3//
#include#include#includevoid inti_board(char board[ROW][COL], int row, int col);//小寫因?yàn)椴慌cROW COL沖突
void print_board(char board[ROW][COL],int row,int col);
void player_move(char board[ROW][COL], int row, int col);
void computer_move(char board[ROW][COL], int row, int col);
char is_win(char board[ROW][COL], int row, i
void test()//選擇游戲是否開始與結(jié)束
{
srand((unsigned)time(NULL));//該函數(shù)為庫函數(shù)且不能多次使用,用于電腦隨機(jī)生成數(shù)字
int a = 0;
do
{
menu();
printf("請選擇\n");
scanf("%d", &a);
switch (a)
{
case 1: printf("三子棋\n");
game();
break;
case 2: printf("退出游戲\n");
break;
default:printf("重新選擇\n");
}
} while (a);
}
2.menu()void menu()//給用戶觀看的菜單
{
printf("****************\n");
printf("** 1.play ***\n");
printf("** 0.exit ***\n");
printf("****************\n");
}
3.inti_board(board,ROW,COL)void inti_board(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]=' ';
}
}
}
4.print_board(board, ROW, COL)void print_board(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++)
{
printf(" %c ", board[i][j]);
if (j< col-1)//每行最后一列不用打印分割行
printf("|");
}
printf("\n");
if (i< row - 1)//最后一行不用打印分割行
for (j = 0; j< col; j++)
{
printf("---");
if (j< col-1)//每行最后一列不用打印分割行
printf("|");
}
printf("\n");
}
}
圖片:
5.player_move(board, ROW, COL)void player_move(char board[ROW][COL], int row, int col)//玩家下棋
{
printf("玩家下棋\n");
while (1)
{
printf("請輸入坐標(biāo)\n");
int x, y;
scanf("%d %d", &x, &y);
if (x - 1 >= 0 && x - 1< row && y - 1< col && y - 1 >= 0)//在合法坐標(biāo)內(nèi)才可下棋
{
if (board[x - 1][y - 1] == ' ')//判斷該坐標(biāo)是否有棋
{
board[x - 1][y - 1] = '*';//玩家下棋
break;
}
else
printf("該坐標(biāo)已被占用,請重新輸入\n");
}
else
printf("非法坐標(biāo)重新輸入\n");
}
}
6.computer_move(board, ROW, COL);void computer_move(char board[ROW][COL], int row, int col)//電腦下棋
{printf("電腦輸入\n");
while (1)
{int x = rand() % row;//該函數(shù)為庫函數(shù)隨機(jī)生成坐標(biāo)
int y = rand() % col;
if (board[x ][y ] == ' ')
{ board[x][y] = '#';//電腦下棋
break;
}
}
}
7.is_win(board, ROW, COL)//該函數(shù)已優(yōu)化可以實(shí)現(xiàn)N×N子棋;char is_win(char board[ROW][COL], int row, int col)//判斷是否輸贏,返回值為一個字符
{
int flag = 0;
int i = 0;
int j = 0;
for (i = 0; i< row; i++)//共row行
{
flag = 0;
for (j = 1; j< col; j++)//對比col-1次
{
if (board[i][j - 1] == board[i][j] && board[i][j - 1] != ' ' && board[i][j] != ' ')//判斷一行中左右坐標(biāo)是否相等且是否為空
{
flag++ ;//如果前后坐標(biāo)相等且不是空坐標(biāo)則加1
}
}
if (flag == col - 1)//共對比col-1次,如果flag加了col-1次說明一行中坐標(biāo)全都相等且不為空坐標(biāo)
return board[i][j - 1];返回該坐標(biāo)的字符
}
//(同上思路)
flag = 0;//重新賦值
for (j = 0; j< col; j++)//循環(huán)col次
{
flag = 0;
for ( i= 1; i< row; i++)//外循環(huán)一次內(nèi)循環(huán)row-1次
{
if (board[i - 1][j] == board[i][j] && board[i - 1][j] != ' ' && board[i - 1][j] != ' ')//判斷一列中上下坐標(biāo)是否相等,且是否為空
flag ++;//如果上下坐標(biāo)相等且不是空坐標(biāo)則加1
}
if (flag == row - 1)//共對比row-1次,如果flag加了row-1次說明一列中坐標(biāo)全都相等且不為空坐標(biāo)
return board[i-1][j];返回該坐標(biāo)的字符
}
flag = 0;
for (i = 0; i< row-1; i++)
{
if (board[i][i] != board[i + 1][i + 1] || board[i][i] == ' ' || board[i + 1][i + 1] == ' ')//從左上到右下對角線的兩兩對比
{
flag = 1;
}
}
if (flag == 0)
return board [i-1][i-1];
flag = 0;
for (i = 1; i< row; i++)
{
if (board[i-1][row - 1-(i-1) ] != board[i ][row-i-1] || board[i-1][row - 1 -(i-1)] == ' ' || board[i] [row-i-1]== ' ')//從右上到左下的兩兩對比
flag = 1;//如果其中有一個對比失敗flag為1
}
if(flag==0)//說明row-1次對比都相等且不為空坐標(biāo)
return board[row-1][0];
if (is_full(board, ROW, COL) == 1)//在玩家和電腦都沒贏的情況下,該函數(shù)判斷棋盤受否滿了,因?yàn)樵摵瘮?shù)為is_win所使用,所以不用在中聲明
{
return 'Q';
}
return 'c';
}
圖片:
圖片:
8.is_full(board, ROW, COL)int is_full(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++)
{
if (board[i][j] == ' ')//如果有一個空坐標(biāo)返回0
return 0;
}
}
return 1;
}
三. 學(xué)習(xí)雞湯每個人都有完美的夢想,只有堅持,你的夢想才能實(shí)現(xiàn),
為了明天的offer加油,加油?。?!
你是否還在尋找穩(wěn)定的海外服務(wù)器提供商?創(chuàng)新互聯(lián)www.cdcxhl.cn海外機(jī)房具備T級流量清洗系統(tǒng)配攻擊溯源,準(zhǔn)確流量調(diào)度確保服務(wù)器高可用性,企業(yè)級服務(wù)器適合批量采購,新人活動首月15元起,快前往官網(wǎng)查看詳情吧