C語言三子棋

2021-09-25 08:47:52 字數 1561 閱讀 1018

1,遊戲開始時,進行初始化棋盤,把所有的元素都設為『 』。

2,提示玩家落子(輸入乙個座標)

3,判定勝負

4,電腦落子(基於隨機數的方式生成乙個座標)

5,判定勝負

#define _crt_secure_no_warnings

#include #include #include int menu()

#define max_row 3

#define max_col 3

char chess_board[max_row][max_col]; //使用巨集定義棋盤大小

void init()

} //設定隨機種子

srand((unsigned int)time(0));

}void print()

}void playermove()

if (chess_board[row][col] != ' ')

chess_board[row][col] = 'x';

break;

} printf("玩家落子完畢\n");

}int isfull()

} }return 1;

}//返回值表示勝利者是誰

//x表示玩家勝

//o表示電腦勝

//q表示和棋

//『 』表示勝負未分

char checkwinner()

//檢查所有列是否連成一條線

for (int col = 0; col <= max_col; ++col)

//檢查所有對角線是否連成一條線

if (chess_board[0][0] == chess_board[1][1]

&& chess_board[0][0] == chess_board[2][2])

if (chess_board[0][2]==chess_board[1][1]

&& chess_board[0][2]==chess_board[2][2])

//棋盤滿,未分出勝負

if (isfull())

return ' ';

}void computermove()

chess_board[row][col] = 'o';

break;

} printf("電腦落子完畢!\n");

}//自頂向下式的程式開發方法

void game()

//4,電腦落子

computermove();

//6,檢測勝負

winner=checkwinner();

if (winner !=' ')

}print();

if (winner == 'x')

else if (winner == 'o')

else if (winner == 'q')

else

}int main()else if (choice == 2)

else

} system("pause");

return 0;

}

C語言 三子棋

使用工具 vs2017 分為三部分 game.h 函式宣告 game.c 實現函式功能 test.c main函式 棋盤為3 3的矩陣 規則 1.每回合玩家與電腦只能放置一枚棋子 2.已有棋子的位置不能再放置棋子 3.若有一方的三枚棋子可連城一條直線,則勝利 4.若棋盤棋子已經布滿,但是雙方沒有一方...

三子棋(c語言)

今天做了乙個三子棋小遊戲,寫了好久卻只完成了一部分,先把這部分分享給大家吧!望大家給點建議和指導,最後判斷輸贏部分到現在還沒構思出來,嘿嘿!game.h define crt secure no warnings 1 ifndef game h define game h include inclu...

c語言 三子棋

c語言三子棋 來完善一下三子棋 game.h define crt secure no warnings 1 ifndef game h define game h include include include include define row 3 define col 3 void menu...