實現三子棋遊戲
阿新 • • 發佈:2018-12-16
1. 首先在game.c中構建一個選單,供玩家選擇,提供兩個選項:1.play 0.exit ,簡單測試一下能否編譯除錯。
#define _CRT_SECURE_NO_WARNINGS #include"game.h" void menu() { printf("############################\n"); printf("####### 1. paly #######\n"); printf("####### 0. exit #######\n"); printf("############################\n"); } void game() { printf("開始遊戲"); }
#define _CRT_SECURE_NO_WARNINGS #include"game.h" int main() { int input = 0; menu(); do { printf("請選擇>:"); scanf("%d", &input); switch (input) { case 0: break; case 1: game(); break; default: printf("選擇有誤,請重新選擇>:"); break; } } while (input); printf("退出遊戲,遊戲結束!"); system("pause"); return 0; }
2. 完善game.h中的函式,並在game.c中實現具體功能。
構建和初始化棋盤、列印棋盤、玩家走、電腦走、判斷輸贏或是平局等模組。
#pragma once #ifndef __GAME_H__ #define _GAME_H_ #define ROW 3 #define COL 3 #include <stdio.h> #include<stdlib.h> #include<string.h> void menu(); void game(); void Create_board(char board[ROW][COL], int roW,int col); void Print_board(char board[ROW][COL], int row, int col); void Player(char board[ROW][COL], int row, int col); void Compurter(char board[ROW][COL], int row, int col); char Iswin(char board[ROW][COL], int row, int col); int Full(char board[ROW][COL], int row, int col); #endif
3. 在game.c裡填充函式內容
~~~建立棋盤(初始化為空格)
void Create_board(char board[ROW][COL], int row, int col)
{
memset(&board,' ',row*col*sizeof(board[0][0]));//把陣列全部初始化成空格
}
~~~列印棋盤
void Print_board(char board[ROW][COL], int row, int col)
{
for (int i = 0; i<row; i++)
{
for (int j = 0; j<col; j++)
{
printf(" %c ", board[i][j]);
if (j<row - 1)
{
printf("|");
}
}
printf("\n");
if (i<row - 1)
{
for (int j = 0; j<col; j++)
{
printf("---");
if (j<row - 1)
{
printf("|");
}
}
printf("\n");
}
}
}
~~~玩家走
void Player(char board[ROW][COL], int row, int col)
{
while (1)
{
int x, y;
printf("請玩家輸入座標>:\n");
scanf("%d %d", &x, &y);
if ((x > 0 && x <row) || y > 0 && y <col)
{
if (board[x][y] != ' ')
{
board[x][y] = 'X';
break;
}
else
{
printf("座標被佔用,重新輸入>:\n");
}
}
printf("輸入座標有誤,重新輸入>:\n");
}
}
~~~電腦走
void Compurter(char board[ROW][COL], int row, int col)
{
while (1)
{
int x, y;
x = rand() % row;//產生0-(row-1)的隨機數
y = rand() % col;
if (board[x][y] != ' ')
{
board[x][y] = '0';
break;
}
}
}
~~~棋盤是否已滿
int Full(char board[ROW][COL], int row, int col)//1表示滿
{
for (int i = 0; i<row; i++)
{
for (int j = 0; j<col; j++)
{
if (board[i][j] == ' ')
{
return 0;
}
}
}
return 1;
}
~~~判斷輸贏
char Iswin(char board[ROW][COL], int row, int col)
{
for (int i = 0; i < row; i++)//判斷行相等
{
if (board[i][0] == board[i][1] && board[i][0] == board[i][2])
{
return board[i][0];
break;
}
}
for (int 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];
}
}
for (int i = 1; i < col; i++)//判斷左對角線相等
{
static int count = 0;
if (board[0][0] == board[i][i])
{
count++;
}
if (count == col - 1)
{
return board[0][0];
}
}
for (int i = 0; i <row; i++)//判斷右對角線相等
{
for (int j = col-1; j >=0; j--)
{
static int count = 0;
if (board[0][col-1] == board[i][j])
{
count++;
}
if (count == col - 1)
{
return board[0][col - 1];
}
}
}
if (Full(board, ROW, COL))//平局
{
return -1;
}
return -2;
}
~~~遊戲總控制函式
void game()
{
srand((unsigned int)time(0));
char board[ROW][COL] = {0};
Create_board(board[ROW][COL], ROW, COL );
Print_board(board[ROW][COL], ROW, COL);
char ret;
while (1)
{
Player(board[ROW][COL], ROW, COL);
Print_board(board[ROW][COL], ROW, COL);
ret = Iswin(board[ROW][COL], ROW, COL);
if ('-2' != ret)
{
break;
}
compurter(board[ROW][COL], ROW ,COL);
print_board(board[ROW][COL], ROW ,COL);
ret = Iswin(board[ROW][COL], ROW, COL);
if ('-2' != ret)
{
break;
}
}
if (ret == '-1')
{
printf("平局!");
}
else if ('X' == ret)
{
printf("恭喜你!你贏了>>\n");
}
else if ('0' == ret)
{
printf("很遺憾!你輸了>>\n");
}
}
4.這個程式主要實現功能,沒有注重遊戲質量。比如電腦走是隨機的,沒有刻意堵棋;沒有讓玩家自定義棋子形狀;選單選項中沒有在玩一次和重新開始的選項;這些都是可以完善的點。