在用c語言實現三子棋小遊戲之前,我們應當瞭解三子棋的遊戲規則:在九宮格棋盤上,哪一方三個棋子連成一線(一行或一列或對角線)即判定哪一方勝利。
以下為原始碼:
1.首先列印出選單。
void menu()
{
printf("**********************************\n");
printf("**********************************\n");
printf("***** 0.play *************\n");
printf("***** 1.exit *************\n");
printf("**********************************\n");
}
menu();
do
{
printf("請輸入要選的的選項: ");
scanf("%d",&input);
switch (input)
{
case 0:
game();
break;
case 1:
printf("退出遊戲");
break;
default:
printf("請輸入要選的選項:\n");
break;
}
} while (input);
2.建立一個二維陣列board用來儲存三子棋的元素,並對其進行初始化。
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] = ' ';
}
}
}
3.列印九宮格棋盤。
void Displayboard(char board[ROW][COL], int row, int col) //列印棋盤
{
int i;
int j;
for (i = 0; i < row; i++)
{
for (j = 0; j < col ; j++)
{
printf(" %c ", board[i][j]);
if (j == col - 1)
continue;
printf("|");
}
printf("\n");
if (i < row - 1)
printf("-----------\n");
}
}
4.使用Playermove和Computermove函數來對board陣列進行更改,模擬玩家和電腦落子情況。
void Playermove(char board[ROW][COL]) //玩家輸入座標
{
while (1)
{
int x = 0;
int y = 0;
printf("請玩家輸入座標");
scanf_s("%d %d", &x, &y);
if (x <= ROW && x >= 1 && y <= COL && y >= 1) //判斷輸入的座標是否越界
{
if (board[x - 1][y - 1] == ' ') //輸入的座標必須為空才可以落子
{
board[x - 1][y - 1] = '*';
break;
}
else
printf("座標被佔用,請重新輸入\n");
}
else
printf("座標有誤,請重新輸入\n");
}
}
void Computermove(char board[ROW][COL]) //電腦輸入座標
{
int x = 0;
int y = 0;
while (1)
{
x = rand() % 3; //使用rand函數生成亂數並取餘得到電腦落子座標
y = rand() % 3;
if (board[x][y] == ' ')
{
printf("電腦輸入座標%d %d\n", x+1, y+1);
board[x][y] = '#';
break; //列印後跳出迴圈
}
}
}
5.玩家和電腦每落一次子,就要進行一次判斷是否獲勝,根據返回值來進行判斷遊戲結果。
int Iswin(char board[ROW][COL], int row, int col) //判斷誰獲勝
{
int i;
int j;
//1為玩家獲勝 0為電腦獲勝 2為平局
for (i = 0; i < row; i++)
{
if (board[i][0] == board[i][1] && board[i][1] == board[i][2] && board[i][1] == '*') //判斷同一行是否相等
return 1;
else if (board[i][0] == board[i][1] && board[i][1] == board[i][2] && board[i][1] == '#')
return 0;
}
for (j = 0; j < col; j++)
{
if (board[0][j] == board[1][j] && board[1][j] == board[2][j] && board[1][j] == '*') //判斷同一列是否相等
return 1;
else if (board[0][j] == board[1][j] && board[1][j] == board[2][j] && board[1][j] == '#')
return 0;
}
if (board[0][0] == board[1][1] && board[1][1] == board[2][2] && board[1][1] == '*') //判斷左對角線是否相等
return 1;
else if (board[0][0] == board[1][1] && board[1][1] == board[2][2] && board[1][1] == '#')
return 0;
if (board[0][2] == board[1][1] && board[1][1] == board[2][0] && board[1][1] == '*') //判斷右對角線是否相等
return 1;
else if (board[0][2] == board[1][1] && board[1][1] == board[2][0] && board[1][1] == '#')
return 0;
if (Full(board, ROW, COL)) //判斷是否平局
return 2;
return 3; //返回3為繼續進行遊戲
}
6.函數宣告,宏定義,函數的定義實現,測試。
#include<stdio.h>
#include<string.h>
#include<time.h>
#include<stdlib.h>
#include"game.h"
# define _CRT_SECURE_NO_WARNINGS
#define ROW 3
#define COL 3
void Initboard(char board[ROW][COL], int row, int col);
void Displayboard(char board[ROW][COL], int row, int col);
void Playermove(char board[ROW][COL]);
void Computer(char board[ROW][COL]);
int Iswin(char board[ROW][COL], int row, int col);
int Full(char board[ROW][COL], int row, int col);
void menu()
{
printf("**********************************\n");
printf("**********************************\n");
printf("***** 0.play *************\n");
printf("***** 1.exit *************\n");
printf("**********************************\n");
}
void game()
{
char board[ROW][COL] = { 0 };
int ret;
Initboard(board, ROW, COL); //用Initboard函數初始化陣列,並用‘ ’初始化陣列
printf("遊戲開始\n");
while (1)
{
Playermove(board); //玩家輸入座標
Displayboard(board, ROW, COL);
ret = Iswin(board, ROW, COL); //判斷是否獲勝
if (ret == 1)
{
printf("玩家獲勝");
break;
}
else if (ret == 0)
{
printf("電腦獲勝");
break;
}
else if (ret == 2)
{
printf("平局");
break;
}
printf("\n\n");
Computer(board); //電腦輸入座標
Displayboard(board, ROW, COL);
ret = Iswin(board, ROW, COL); //判斷是否獲勝
if (ret == 1)
{
printf("玩家獲勝");
break;
}
else if (ret == 0)
{
printf("電腦獲勝");
break;
}
else if (ret == 2)
{
printf("平局");
break;
}
printf("\n\n");
}
}
int main()
{
int input = 0;
srand((unsigned int)time(NULL));
menu();
do
{
printf("請輸入要選的的選項: ");
scanf("%d",&input);
switch (input)
{
case 0:
game();
break;
case 1:
printf("退出遊戲");
break;
default:
printf("請輸入要選的選項:\n");
break;
}
} while (input);
}
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] = ' ';
}
}
}
void Displayboard(char board[ROW][COL], int row, int col) //列印棋盤
{
int i;
int j;
for (i = 0; i < row; i++)
{
for (j = 0; j < col ; j++)
{
printf(" %c ", board[i][j]);
if (j == col - 1)
continue;
printf("|");
}
printf("\n");
if (i < row - 1)
printf("-----------\n");
}
}
void Playermove(char board[ROW][COL]) //玩家輸入座標
{
while (1)
{
int x = 0;
int y = 0;
printf("請玩家輸入座標");
scanf_s("%d %d", &x, &y);
if (x <= ROW && x >= 1 && y <= COL && y >= 1)
{
if (board[x - 1][y - 1] == ' ')
{
board[x - 1][y - 1] = '*';
break;
}
else
printf("座標被佔用,請重新輸入\n");
}
else
printf("座標有誤,請重新輸入\n");
}
}
void Computer(char board[ROW][COL]) //電腦輸入座標
{
int x = 0;
int y = 0;
while (1)
{
x = rand() % 3;
y = rand() % 3;
if (board[x][y] == ' ')
{
printf("電腦輸入座標%d %d\n", x+1, y+1);
board[x][y] = '#';
break; //列印後跳出迴圈
}
}
}
int Iswin(char board[ROW][COL], int row, int col) //判斷誰獲勝
{
int i;
int j;
//1為玩家獲勝 0為電腦獲勝 2為平局
for (i = 0; i < row; i++)
{
if (board[i][0] == board[i][1] && board[i][1] == board[i][2] && board[i][1] == '*') //判斷同一行是否相等
return 1;
else if (board[i][0] == board[i][1] && board[i][1] == board[i][2] && board[i][1] == '#')
return 0;
}
for (j = 0; j < col; j++)
{
if (board[0][j] == board[1][j] && board[1][j] == board[2][j] && board[1][j] == '*') //判斷同一列是否相等
return 1;
else if (board[0][j] == board[1][j] && board[1][j] == board[2][j] && board[1][j] == '#')
return 0;
}
if (board[0][0] == board[1][1] && board[1][1] == board[2][2] && board[1][1] == '*') //判斷左對角線是否相等
return 1;
else if (board[0][0] == board[1][1] && board[1][1] == board[2][2] && board[1][1] == '#')
return 0;
if (board[0][2] == board[1][1] && board[1][1] == board[2][0] && board[1][1] == '*') //判斷右對角線是否相等
return 1;
else if (board[0][2] == board[1][1] && board[1][1] == board[2][0] && board[1][1] == '#')
return 0;
if (Full(board, ROW, COL)) //判斷是否平局
return 2;
return 3; //返回3為繼續進行遊戲
}
int Full(char board[ROW][COL], int row, int col) //判斷棋盤是否下滿
{
int i;
int j;
for (i = 0; i < row; i++)
{
for (j = 0; j < col; j++)
{
if (board[i][j] == ' ')
return 0; //此時棋盤未滿
}
}
return 1; //此時棋盤已滿
}