今日は小さなゲームについて話しましょう。誰もがバックギャモンをプレイしたことがあるはずです。つまり、行と列、または対角線上に同じサイズの文字があれば勝ちです。今日は、これを達成するためのコードを書きますこの機能
まず、2 つのソース ファイルと 1 つのヘッダー ファイルを作成します。
ヘッダー ファイル game.h は、ヘッダー ファイルと関数宣言といくつかの定義を含めるために使用されます。
game.c はゲームを実装するために使用
されます。 test.c はゲームのロジックをテストするために使用されます。
test.c
#include<stdio.h>
void menu()
{
printf("***************\n");
printf("****0.exit*****\n");
printf("****1.play*****\n");
printf("***************\n");
}
void test()
{
int input = 0;
do
{
menu();//打印菜单
printf("请选择>");
scanf("%d", &input);
switch(input)
{
case 0:
printf("退出游戏\n");
break;
case 1:
printf("进入游戏\n");
break;
default:
printf("请重新选择\n");
break;
}
} while (input);
}
int main()
{
test();
return 0;
}
まず印刷メニューのロジックを実装しましょう。
ゲームに入るには 1 を選択し、ゲームを終了するには 0 を選択し、再選択するにはその他を選択します。do
while 関数を使用すると、メニューが来るとすぐに印刷されますここで
ゲームを実装し、1 を選択して完了します。
次のステップでは、3 手ゲームのロジックを実装するための game() 関数を作成します。
まず、チェス盤を初期化する必要があります。
test.c
void game()
{
//存放下棋的数据
char board[Row][Col ] = {
0 };
//初始化棋盘为空格
Initboard(board, Row, Col);
}
ゲーム.h
//初始化棋盘
void Initboard(char board[Row][Col], int row, int col);
ゲーム.c
void Initboard(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++)
{
board[i][j] = ' ';
}
}
}
チェス盤を初期化した後、チェス盤を印刷する必要がありますが、チェス盤の印刷は三目並べのようなものです。
ゲーム.h
//打印棋盘
void Displayboard(char board[Row][Col], int row, int col);
ゲーム.c
void Displayboard(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++)
{
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");
}
}
}
test.c
//打印棋盘
Displayboard(board, Row, Col);
次に私たちがすることはチェスをすることです
ゲーム.h
//玩家下棋
void play_move(char board[Row][Col], int row, int col);
ゲーム.c
void play_move(char board[Row][Col], int row, int col)
{
int x = 0;
int y = 0;
printf("玩家下棋\n");
while (1)
{
printf("请输入坐标>");
scanf("%d %d", &x, &y);
if (x >= 1 && x <= row && y >= 1 && y <= col)
{
if (' ' == board[x - 1][y - 1])
{
board[x - 1][y - 1] = '*';
break;
}
else
{
printf("该内容被占用,请重新选择\n");
}
}
else
{
printf("坐标非法,请重新输入\n");
}
}
}
test.c
play_move(board, Row, Col);
//打印棋盘
Displayboard(board, Row, Col);
プレイヤーがチェスのプレイを終了した後、コンピューターを起動してチェスをプレイしますが、ここで行ったことは単なるランダムなチェスです。
ゲーム.h
void computer_move(char board[Row][Col], int row, int col);
ゲーム.c
void computer_move(char board[Row][Col], int row, int col)
{
int x = 0;
int y = 0;
printf("电脑下棋\n");
while (1)
{
x = rand() % row;
y = rand() % col;
if (board[x][y] == ' ')
{
board[x][y] = '#';
break;
}
}
}
チェスをプレイするコンピュータとチェスをプレイするプレイヤーはサイクルであり、それらはランダムですが、このとき、rand 関数を使用する必要があり、同時に srand とタイムスタンプの概念も使用する必要があります。はすべて「数字の推測」で述べたとおりです。
次は、x = rand() % row; y = rand() % col;
そのような座標をどのように生成するかを考えます。これを考えると、勝ち負けをどのように判断するかを考える必要があります。 3 ゲームのチェスでは、対角線または行と列が満たされなければなりません。その場合、それを達成するためにそのような関数を作成します。
test.c
while (1)
{
//玩家下棋
play_move(board, Row, Col);
Displayboard(board, Row, Col);
ret=is_win(board,Row,Col);
if (ret != 'C')
{
break;
}
//电脑下棋
computer_move(board, Row, Col);
Displayboard(board, Row, Col);
ret = is_win(board, Row, Col);
if (ret != 'C')
{
break;
}
}
if (ret == '*')
{
printf("玩家赢了\n");
}
else if (ret == '#')
{
printf("电脑赢了\n");
}
else
{
printf("平局\n");
}
Displayboard(board, Row, Col);//再次打印棋盘
}
*玩家赢
#电脑赢
C平局
完全なコード
game.h
#pragma once
#include<stdio.h>
#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 play_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);
ゲーム.c
#define _CRT_SECURE_NO_WARNINGS 1
#include"game.h"
void Initboard(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++)
{
board[i][j] = ' ';
}
}
}
void Displayboard(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++)
{
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 play_move(char board[Row][Col], int row, int col)
{
int x = 0;
int y = 0;
printf("玩家下棋\n");
while (1)
{
printf("请输入坐标>");
scanf("%d %d", &x, &y);
if (x >= 1 && x <= row && y >= 1 && y <= col)
{
if (' ' == board[x - 1][y - 1])
{
board[x - 1][y - 1] = '*';
break;
}
else
{
printf("该内容被占用,请重新选择\n");
}
}
else
{
printf("坐标非法,请重新输入\n");
}
}
}
void computer_move(char board[Row][Col], int row, int col)
{
int x = 0;
int y = 0;
printf("电脑下棋\n");
while (1)
{
x = rand() % row;
y = rand() % col;
if (board[x][y] == ' ')
{
board[x][y] = '#';
break;
}
}
}
char is_win(char board[Row][Col], int row, int col)
{
int i = 0;
for (i = 0; i < row; i++)
{
if (board[i][0] == board[i][1] && board[i][1] == board[i][2] && board[i][0] != ' ')
{
return board[i][0];
}
}
}
test.c
#define _CRT_SECURE_NO_WARNINGS 1
//测试三子棋逻辑
#include"game.h"
void game()
{
char ret = 0;
//存放下棋的数据
char board[Row][Col] = {
0 };
//初始化棋盘为空格
Initboard(board, Row, Col);
//打印棋盘
Displayboard(board, Row, Col);
while (1)
{
//玩家下棋
play_move(board, Row, Col);
Displayboard(board, Row, Col);
ret=is_win(board,Row,Col);
if (ret != 'C')
{
break;
}
//电脑下棋
computer_move(board, Row, Col);
Displayboard(board, Row, Col);
ret = is_win(board, Row, Col);
if (ret != 'C')
{
break;
}
}
if (ret == '*')
{
printf("玩家赢了\n");
}
else if (ret == '#')
{
printf("电脑赢了\n");
}
else
{
printf("平局\n");
}
Displayboard(board, Row, Col);
}
void menu()
{
printf("**************\n");
printf("****1.play****\n");
printf("****0.exit****\n");
printf("**************\n");
}
void test()
{
int input = 0;
do
{
menu();
printf("请选择>");
scanf("%d", &input);
switch (input)
{
case 0:
printf("退出游戏\n");
break;
case 1:
game();
break;
default :
printf("请重新输入\n");
break;
}
} while (input);
}
int main()
{
srand((unsigned int)time(NULL));//时间戳
test();
return 0;
}
今日のミニゲームはここまでです、また次回