本文通过实例为大家分享了C语言实现三子棋小游戏的具体代码,供大家参考,具体内容如下
基本思路:
1.创建一个游戏选择面板.
2.创建并初始化棋盘。
3.玩家落子并判定,电脑落子并判定。
4.判定结果 ,游戏结束!
代码如下:
头文件:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
#pragma once #include<stdio.h> #include<windows.h> #include<stdlib.h> #include <time.h> #pragma warning(disable:4996) extern void Game(); //游戏代码需要定义调用的函数 #define ROW 3//FOW、FOL代表三子棋边界的长和宽 #define COL 3 #define INIT ' ' //INIT为空格的宏定义 #define WHITE 'X' //X在三字棋代表Player #define BLACK 'O' //O在三字棋代表Computer #define DRAW 'D' //DRAW代表平局 #define NEXT 'N' //NEXT代表继续 //MineClearence |
各类函数:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
|
void InitBoard( char board[][COL], int row, int col) //棋盘初始化 { for ( int i = 0; i < row; i++){ for ( int j = 0; j < col; j++){ board[i][j] = INIT; } } } static void ShowBoard( char board[][COL], int row, int col) //显示棋盘 { system ( "cls" ); printf ( " " ); for ( int i = 0; i < col; i++){ printf ( "%4d" , i + 1); } printf ( "\n--------------\n" ); for ( int i = 0; i < row; i++){ printf ( "%-2d" , i + 1); //2 for ( int j = 0; j < col; j++){ printf ( "| %c " , board[i][j]); } printf ( "\n--------------\n" ); } } static char IsEnd( char board[][COL], int row, int col) //最终结果 { for ( int i = 0; i < row; i++){ if (board[i][0] == board[i][1] && board[i][1] == board[i][2] && board[i][0] != INIT){ return board[i][0]; } } for ( int j = 0; j < COL; j++){ if (board[0][j] == board[1][j] && board[1][j] == board[2][j] && board[0][j] != INIT){ return board[0][j]; } } if (board[0][0] == board[1][1] && board[1][1] == board[2][2] && board[1][1] != INIT){ return board[1][1]; } if (board[0][2] == board[1][1] && board[1][1] == board[2][0] && board[1][1] != INIT){ return board[1][1]; } for ( int i = 0; i < row; i++){ for ( int j = 0; j < col; j++){ if (board[i][j] == INIT){ return NEXT; } } } return DRAW; } static void PlayerMove( char board[][COL], int row, int col) //玩家 { int x = 0; int y = 0; while (1){ printf ( "Please Enter Postion<x,y># " ); scanf ( "%d %d" , &x, &y); if (x < 1 || y < 1 || x > 3 || y > 3){ printf ( "Enter Postion Error!\n" ); continue ; } if (board[x - 1][y - 1] == INIT){ board[x - 1][y - 1] = WHITE; break ; } else { printf ( "Postion Is Not Empty!\n" ); } } } static void ComputerMove( char board[][COL], int row, int col) //电脑 { while (1){ int x = rand () % row; int y = rand () % col; if (board[x][y] == INIT){ board[x][y] = BLACK; break ; } } } void Game() { char board[ROW][COL]; InitBoard(board, ROW, COL); srand ((unsigned long ) time (NULL)); char result = 0; while (1){ ShowBoard(board, ROW,COL); PlayerMove(board, ROW,COL); result = IsEnd(board, ROW, COL); if (result != NEXT){ break ; } ShowBoard(board, ROW, COL); ComputerMove(board,ROW, COL); result = IsEnd(board, ROW,COL); if (result != NEXT){ break ; } } ShowBoard(board, ROW,COL); switch (result){ case WHITE: printf ( "You Win!\n" ); break ; case BLACK: printf ( "You Lose!\n" ); break ; case DRAW: printf ( "You == Computer!\n" ); break ; default : printf ( "BUG!\n" ); break ; } } |
游戏选择面板(主函数)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
|
#include "game.h" static void Menu() { printf ( "|------------- 开始游戏 -------------|\n" ); printf ( "+------------- 1. Play -------------+\n" ); printf ( "+------------- 0. Exit -------------+\n" ); } int main() { int select = 0; int quit = 0; while (!quit){ Menu(); printf ( "Please Select#" ); scanf ( "%d" ,&select); switch (select){ case 1: Game(); break ; case 0: quit = 1; break ; default : printf ( "Enter Error, Try Again!\n" ); break ; } } printf ( "bye!\n" ); system ( "pause" ); return 0; } |
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。
原文链接:https://blog.csdn.net/m0_53146328/article/details/117817704