三子棋游戲
問題描述:
3*3的棋盤中,只要一條線上出現三個一樣的棋子就獲勝(玩家或電腦);如果棋盤已經放滿還未出現三個棋子一條線則打成平手,
具體細節:
- 初始化棋盤(用空格初始化)
//初始化棋盤
void initChess(char chessbox[ROW][COL]){
for (int row = 0; row < ROW; row++){
for (int col = 0; col < COL; col++){
chessbox[row][col] = ' ';
}
}
}
- 列印棋盤
//列印棋盤
void printChess(char chessbox[ROW][COL]){
system("cls");
printf("+---+---+---+\n");
for (int row = 0; row < ROW; row++){
printf("| %c | %c | %c |\n",
chessbox[row][0], chessbox[row][1],
chessbox[row][2]);
printf("+---+---+---+\n");
}
}
- 電腦落子(用o表示電腦落子)
//電腦落子(用o表示)
void computerMove(char chessbox[ROW][COL]){
srand(time(0));
while (1){
int row = rand() % 3;
int col = rand() % 3;
if (chessbox[row][col] == ' '){
chessbox[row][col] = 'o';
break;
}
}
}
- 玩家落子
//玩家落子(用x表示)
void playerMove(char chessbox[ROW][COL]){
int row, col;
while (1){
printf("請輸入您的落子地點:");
scanf("%d %d", &row, &col);
if (row >= 3 || col >= 3){
printf("您輸入的落子位置有誤,請重新輸入:");
continue;
}
if (chessbox[row][col] == ' '){
chessbox[row][col] = 'x';
break;
}
printf("該位置已有棋子,請重新輸入:");
}
}
- 三個棋子一條線
- 在一行或一列實作三個棋子一條線
//行
for (int row = 0; row < ROW; row++){
if (chessbox[row][0] != ' '
&&chessbox[row][0] == chessbox[row][1]
&& chessbox[row][0] == chessbox[row][2]){
return chessbox[row][0];
}
}
//列
for (int col = 0; col < COL; col++){
if (chessbox[0][col] != ' '
&&chessbox[0][col] == chessbox[1][col]
&& chessbox[0][col] == chessbox[2][col]){
return chessbox[0][col];
}
}
- 對角線實作三個棋子一條線
if (chessbox[0][0] != ' '
&&chessbox[0][0] == chessbox[1][1]
&& chessbox[0][0] == chessbox[2][2]){
return chessbox[0][0];
}
if (chessbox[2][0] != ' '
&&chessbox[2][0] == chessbox[1][1]
&& chessbox[2][0] == chessbox[0][2]){
return chessbox[2][0];
}
- 和棋
- 棋盤放滿還未獲勝,則為和棋,打成了平手,
在這里插入代碼片
//和棋
if(isFull(checkbox)){
return 'a';
}
- 輸贏約定:
- 回傳x代表玩家獲勝
if (isWinner(chessbox) == 'x'){
printf("恭喜您贏啦!\n");
break;
}
- 回傳o代表電腦獲勝
if (isWinner(chessbox) == 'o'){
printf("很遺憾,您輸了!\n");
break;
}
- 回傳a代表和棋(打成平手)
if (isWinner(chessbox) == 'a'){
printf("你和電腦同一水平呦!\n");
break;
}
- 判斷棋盤是否放滿:
- 回傳1代表棋盤已滿
- 回傳0代表棋盤未滿
//判斷棋盤是否擺滿
//1表示滿;0表示不滿,
int isFullChess(char chessbox[ROW][COL]){
for (int row = 0; row < ROW; row++){
for (int col = 0; col < COL; col++){
//找到空格,說明未滿
if (chessbox[row][col] == ' '){
return 0;
}
}
}
return 1;
}
源代碼:
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#define ROW 3
#define COL 3
//玩家落子(用x表示)
void playerMove(char chessbox[ROW][COL]){
int row, col;
while (1){
printf("請輸入您的落子地點:");
scanf("%d %d", &row, &col);
if (row >= 3 || col >= 3){
printf("您輸入的落子位置有誤,請重新輸入:");
continue;
}
if (chessbox[row][col] == ' '){
chessbox[row][col] = 'x';
break;
}
printf("該位置已有棋子,請重新輸入:");
}
}
//電腦落子(用o表示)
void computerMove(char chessbox[ROW][COL]){
srand(time(0));
while (1){
int row = rand() % 3;
int col = rand() % 3;
if (chessbox[row][col] == ' '){
chessbox[row][col] = 'o';
break;
}
}
}
//初始化棋盤
void initChess(char chessbox[ROW][COL]){
for (int row = 0; row < ROW; row++){
for (int col = 0; col < COL; col++){
chessbox[row][col] = ' ';
}
}
}
//列印棋盤
void printChess(char chessbox[ROW][COL]){
system("cls");
printf("+---+---+---+\n");
for (int row = 0; row < ROW; row++){
printf("| %c | %c | %c |\n",
chessbox[row][0], chessbox[row][1],
chessbox[row][2]);
printf("+---+---+---+\n");
}
}
//判斷棋盤是否擺滿,擺滿回傳1;未滿回傳0
int isFullChess(char chessbox[ROW][COL]){
for (int row = 0; row < ROW; row++){
for (int col = 0; col < COL; col++){
//找到空格,說明未滿
if (chessbox[row][col] == ' '){
return 0;
}
}
}
return 1;
}
//約定:回傳x代表玩家贏了;回傳o代表電腦贏了;回傳a代表和棋
char isWinner(char chessbox[ROW][COL]){
//行
for (int row = 0; row < ROW; row++){
if (chessbox[row][0] != ' '
&&chessbox[row][0] == chessbox[row][1]
&& chessbox[row][0] == chessbox[row][2]){
return chessbox[row][0];
}
}
//列
for (int col = 0; col < COL; col++){
if (chessbox[0][col] != ' '
&&chessbox[0][col] == chessbox[1][col]
&& chessbox[0][col] == chessbox[2][col]){
return chessbox[0][col];
}
}
//對角線
if (chessbox[0][0] != ' '
&&chessbox[0][0] == chessbox[1][1]
&& chessbox[0][0] == chessbox[2][2]){
return chessbox[0][0];
}
if (chessbox[2][0] != ' '
&&chessbox[2][0] == chessbox[1][1]
&& chessbox[2][0] == chessbox[0][2]){
return chessbox[2][0];
}
//和棋
if (isFullChess(chessbox)){
return 'a';
}
return 0;
}
//開始一局游戲
void game(){
char chessbox[ROW][COL] = { 0 };
initChess(chessbox);
printf("游戲開始啦!\n");
printChess(chessbox);
while (1){
//玩家落子
playerMove(chessbox);
//列印棋牌
printChess(chessbox);
//判斷勝負
if (isWinner(chessbox) == 'x'){
printf("恭喜您贏啦!\n");
break;
}
if (isWinner(chessbox) == 'a'){
printf("你和電腦同一水平呦!\n");
break;
}
//電腦落子
computerMove(chessbox);
//列印棋牌
printChess(chessbox);
//判斷勝負
if (isWinner(chessbox) == 'o'){
printf("很遺憾,您輸了!\n");
break;
}
if (isWinner(chessbox) == 'a'){
printf("你和電腦同一水平呦!\n");
break;
}
}
}
int menu(){
printf("===============\n");
printf("1.開始游戲\n");
printf("0.結束游戲\n");
printf("===============\n");
int choice;
printf("請輸入您的選擇:");
scanf("%d", &choice);
return choice;
}
int main()
{
ile (1){
int choice=menu();
if (choice == 1){
game();
continue;
}
else if (choice == 0){
break;
}else{
printf("輸入有誤,請您重新輸入!\n");
continue;
}
}
system("pause");
return 0;
}
運行結果:




轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/206303.html
標籤:其他
