当前位置:网站首页>Game (sanziqi & minesweeping)
Game (sanziqi & minesweeping)
2022-06-23 01:42:00 【'Dream_】
If you will It's better to start than to give up . Roman.
May we all have our own goals and are making unremitting efforts for them .
---------------------------------------------------------------------------------------
One 、 Sanzi
One ) Analysis and preparation
1、
test.c // Test the logic of the game
game.c // The realization of the game
game.h // Declaration of functions ( need A semicolon ;) And the definition of symbols
( because #include<stdio.h> Use... In all three files , Therefore, you can directly include it in the header file and reference the header file )
2、 game part
Game layout :
menu choice ( At least one cycle )
Design the game :
Store chess data : A two-dimensional array of characters ( In order to change the value , Define rows and columns in the header file )
The board : Initialize the chessboard to full space
Print chessboard : data Split line
Players play chess :PlayerMove( )
Enter coordinates --》 Judge whether the coordinates are legal ( loop ) --》 Determine whether it is occupied --》 Playing chess
Print chessboard DisplayBoard( )
Judgement of winning or losing :Iswin( )
- Use the return value to judge whether you win or lose -- Game player wins * Computers win # It ends in a draw Q continue C
- win or It ends in a draw IsFull( ): A draw exists only in game.c in , Other functions cannot be used , be static modification
The computer plays chess :ComputerMove( ) // Random chess
Random value rand( ) The acquisition of needs srand((unsigned int)time(NuLL)) Put it in test.c In file , Note the range of random values , Need header file stdlib.h time.h
Print chessboard
Judgement of winning or losing
( notes : Players play chess with computers in a circular way , Until one side wins or A draw is over )
Print the chessboard for the last time ( not essential )
3、 Some naming conventions
initialization :InitBoard( )
Print :DisplayBoard( )
That's ok :int row
Column :int col
Two ) Code implementation
1、test.c file
#include"game.h"
// menu
void menu()
{
printf("*****************************************************\n");
printf("********* Welcome to my game word! **********\n");
printf("********* 1. play **********\n");
printf("********* 0. exit **********\n");
printf("*****************************************************\n");
}
int main()
{
// Create array
char chess[Row][Col] = { 0 };
int row = 3;
int col = 3;// Pay attention to it main Function
int choi = 0;
do
{
srand((unsigned int)time(NULL));// One update at a time
// menu
menu();
printf(" Please enter your choice :\n");
scanf("%d", &choi);
// Judgment input
switch (choi)
{
case 1:
printf("Game Start!\n");
// game
game();
break;
case 0:
printf(" Quit the game !\n");
break;
default:
printf(" illegal input Please re-enter !\n");
break;
}
} while (choi);
return 0;
}2、game.c file
#include"game.h"
// Initialize chessboard : All data are 0
void InitBoard(char chess[Row][Col], int row, int col)
{
int i = 0;
for (i = 0; i < row; i++)
{
int j = 0;
for (j = 0; j < col; j++)
{
chess[i][j] = ' ';
}
}
}
// Print chessboard
void DisplayBoard(char chess[Row][Col], int row, int col)
{
int i = 0;
for (i = 0; i < row; i++)
{
int j = 0;
// Print data
for (j = 0; j < col; j++)
{
if (j < col - 1)
{
printf(" %c |", chess[i][j]);
}
else
{
printf(" %c ", chess[i][j]);
}
}
printf("\n");
// Print split lines
if (i < row - 1)
{
for (j = 0; j < col; j++)
{
printf("___");
if (j < col - 1)
{
printf("|");
}
}
printf("\n");
}
else
{
for (j = 0; j < col; j++)
{
printf(" ");
if (j < col - 1)
{
printf("|");
}
}
printf("\n");
}
}
}
// Judge whether to win
char IsWin(char chess[Row][Col], int x, int y, int row, int col)
{
// win
// Same row
int i = 0;
for (i = 0; i < row; i++)
{
//if (chess[i][0] == chess[i][1] == chess[i][2] == '*')// Wrong writing !!
if((chess[i][0]==chess[i][1])&&(chess[i][1]==chess[i][2])&&(chess[i][0]=='*'))
{
return '*';
}
else if ((chess[i][0] == chess[i][1]) && (chess[i][1] == chess[i][2]) && (chess[i][0] == '#'))
{
return '#';
}
}
// The columns are the same
int j = 0;
for (j = 0; j < col; j++)
{
if ((chess[0][j] == chess[1][j]) && (chess[1][j] == chess[2][j]) && (chess[0][j] == '*'))
{
return '*';
}
else if ((chess[0][j] == chess[1][j]) && (chess[1][j] == chess[2][j]) && (chess[0][j] == '#'))
{
return '#';
}
}
// The diagonals are the same
if ((chess[0][0] == chess[1][1])&&(chess[1][1] == chess[2][2])&&(chess[0][0] == '*'))
{
return '*';
}
else if ((chess[0][0] == chess[1][1]) && (chess[1][1] == chess[2][2]) && (chess[0][0] == '#'))
{
return '#';
}
if ((chess[0][2] == chess[1][1]) && (chess[1][1] == chess[2][0]) && (chess[0][2] == '*'))
{
return '*';
}
else if((chess[0][2] == chess[1][1]) && (chess[1][1] == chess[2][0]) && (chess[0][2] == '#'))
{
return '#';
}
// The game goes on
for (i = 0; i < row; i++)
{
for (j = 0; j < col; j++)
{
if (chess[i][j] == ' ')
{
return 'C';
}
}
}
}
// Players move
char PlayerMove(char chess[Row][Col], int row, int col)
{
int x = 0;
int y = 0;
while (1)
{
printf(" Players move ( Please enter the coordinates ):\n");
scanf("%d %d", &x, &y);
if (chess[x-1][y-1] == ' ')
{
chess[x-1][y-1] = '*';// Players play chess pieces *
}
else if ((x > row) || (y > col))
{
printf(" This location is out of range , Please re-enter !\n");
continue;
}
else
{
printf(" This place has been occupied , Please re-enter !\n");
continue;
}
// Print chessboard
DisplayBoard(chess, Row, Col);
// Judgement of winning or losing
char iw = IsWin(chess, x, y, row, col);
return iw;
}
}
Computer action
char ComputerMove(char chess[Row][Col], int row, int col)
{
printf(" The computer plays chess :\n");
while (1)
{
// Generate random number
int x = rand() % row;
int y = rand() % col;
if (chess[x][y] == ' ')
{
chess[x][y] = '#';// The computer plays chess '#'
}
else
{
continue;
}
// Print chessboard
DisplayBoard(chess, Row, Col);
// Judgement of winning or losing
char iw = IsWin(chess, x, y, row, col);
return iw;
}
}
// game
void game()
{
// Create array
char chess[Row][Col] = { 0 };
// Initialize chessboard : All data are 0
InitBoard(chess, Row, Col);
// Print chessboard
DisplayBoard(chess, Row, Col);
while (1)
{
// Players move
char iw1 = PlayerMove(chess, Row, Col);
Print chessboard
//DisplayBoard(chess, Row, Col);
if (iw1 == '*')
{
printf(" Game player wins \n");
Sleep(1000);
printf("\n Do you want another round ?\n");
break;
}
else if (iw1 == '#')
{
printf(" Computers win \n");
Sleep(1000);
printf("\n Do you want another round ?\n");
break;
}
else if (iw1 == 'C')
{
;// The game goes on
}
else
{
printf(" It ends in a draw \n");
Sleep(1000);
printf("\n Do you want another round ?\n");
break;
}
// Computer action
char iw2 = ComputerMove(chess, Row, Col);
Print chessboard
//DisplayBoard(chess, Row, Col);
if (iw2 == '*')
{
printf(" Game player wins \n");
Sleep(1000);
printf("\n Do you want another round ?\n");
break;
}
else if (iw2 == '#')
{
printf(" Computers win \n");
Sleep(1000);
printf("\n Do you want another round ?\n");
break;
}
else if (iw2 == 'C')
{
;// The game goes on
}
else
{
printf(" It ends in a draw \n");
Sleep(1000);
printf("\n Do you want another round ?\n");
break;
}
}
}3、game.h file
#pragma once
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#include<windows.h>
#define Row 3
#define Col 3
//int row = 3;
//int col = 3;
// game
void game();
// Initialize chessboard : All data are 0
void InitBoard(char chess[Row][Col], int row, int col);
// Print chessboard
void DisplayBoard(char chess[Row][Col], int row, int col);
// Players move
char PlayerMove(char chess[Row][Col], int row, int col);
// Judge whether to win
char IsWin(char chess[Row][Col], int x, int y, int row, int col);
// Computer action
char ComputerMove(char chess[Row][Col], int row, int col);******************************************************************************
Two 、 Mine clearance
One ) Analysis and preparation
1、
test.c // Test the logic of the game
game.c // The realization of the game
game.h // Declaration of functions ( need A semicolon ;) And the definition of symbols
( because #include<stdio.h> Use... In all three files , Therefore, you can directly include it in the header file and reference the header file )
2、 game part
menu Judge ( loop ) game
The realization of the game :
Arrange thunder : Two dimensional array
Lay out Ray's information : Another new array mine( Multiple rows and columns :1 It means ray 0 It means no thunder )
An array of information about the mine show( Same multi row and multi column * Indicates that... Is not checked The number is the number of minefields around )
// Define rows and columns
// Define an array
// Initialize two arrays : Set one more parameter to initialize the content set
// Print chessboard show_board( );
// Arrange thunder :set_mine( );
The number of mines is defined in the header file , Then assign it to count; The coordinates take random values , And loop mine , until count==0
// Print show The board ( Remain mysterious )
// Check the thunder :find_mine( );
Mine clearance :
Enter coordinates : Judge whether it is legal ( loop )
It's ray , killing , Game over , Print mine; It's not ray , Tell them how many mines there are in the eight coordinates around them get_mine( ) function : Numeric character - Numbers 0 character == Corresponding number , Print show
Until we find the location of all the mines , Then the game is over , Mine clearance succeeded ;
Number of demining == Grid number - Thunder number EASY_COUNT
3、 Extend the functionality
1) Mark : Mark ray
2) Disappear : Recursive implementation : The selected one is not thunder and there is no thunder around , This coordinate has not been checked , Then continue recursion , Mark the number of Mines until there are mines around
Be careful : Recursion cannot have dead recursion , namely : You cannot recurse what has already occurred , Mark what has already appeared
Two ) Code implementation
1、test.c file
// Simple minesweeping 9 That's ok 9 Column
//10 A thunder
#include"game.h"
// menu
void menu()
{
printf("*******************************************************\n");
printf("************* Welcome To My Game! *************\n");
printf("************* 1.Play *************\n");
printf("************* 0.Exit *************\n");
printf("*******************************************************\n");
}
void game()
{
// Set array
char mine[ROWS][COLS] = { 0 };
char show[ROWS][COLS] = { 0 };
Initialize array
//init_mine(mine, ROWS, COLS);
//init_show(show, ROW, COL);
// Initialize array Optimize
Init(mine, ROWS, COLS, '0');
Init(show, ROWS, COLS, '*');
// Print show
Print(show, ROW, COL);
// Arrange thunder
set_mine(mine, ROW, COL);
Check the settings
//Print(mine, ROWS, COLS);
int sum = ROW * COL;
// loop
while (sum > EASY_COUNT)
{
// Mine clearance
int num = find_mine(mine, show, ROW, COL);
// Judge whether it is thunder
if (9 == num)
{
// The layout of Lei should be printed here : With show Array for printing
Print(show, ROW, COL);
printf(" I'm sorry You were killed !\n");
break;
}
else
{
// Non thunder
sum--;
// The layout of Lei should be printed here : With show Array for printing
Print(show, ROW, COL);
}
}
if (sum == EASY_COUNT)
{
printf(" congratulations Game wins !\n");
}
}
int main()
{
//
int row = 9;
int col = 9;
int rows = 11;
int cols = 11;
do
{
// Random number preparation
srand((unsigned int)time(NULL));
// menu
menu();
// choice
int choi = 0;
printf(" Please enter your choice :\n");
scanf("%d", &choi);
switch (choi)
{
case 1:
printf("Game Start!\n");
game();
Sleep(1000);
printf(" Do you want another round ?\n");
printf("\n");
break;
case 0:
printf("Game Over!\n");
break;
default:
printf(" illegal input Please re-enter !\n");
printf("\n");
break;
}
}while(1);
return 0;
}2、game.c file
#include"game.h"
//
Initialize two arrays
//void init_mine(char mine[ROWS][COLS],int rows,int cols)
//{
// int i = 0;
// for (i = 0; i < rows; i++)
// {
// int j = 0;
// for (j = 0; j < cols; j++)
// {
// mine[i][j] = '0';// Initialize mine free 0
// }
// }
//}
//
//void init_show(char show[ROW][ COL], int row, int col)
//{
// // Keep the mystery *
// int i = 0;
// for (i = 0; i < row; i++)
// {
// int j = 0;
// for (j = 0; j < col; j++)
// {
// show[i][j] = '*';// initialization *
// }
// }
//}
// Initialize two arrays Optimize
void Init(char arr[ROWS][COLS],int rows,int cols, char set)
{
int i = 0;
for (i = 0; i < rows; i++)
{
int j = 0;
for (j = 0; j < cols; j++)
{
arr[i][j] = set;// Initialize mine free 0
}
}
}
Print function
//void Print(char arr[ROW][COL], int x, int y)
//{
// int i = 0;
// int j = 0;
//
// //
// for (i = 0; i <= x; i++)
// {
// for (j = 0; j <= y; j++)
// {
// if (0 == i)
// {
// printf("%d ", j);
// }
// else
// {
// if (j < y)
// {
// printf("%c ", arr[i - 1][j]);
// }
// }
// }
// printf("\n");
// if (i < x)
// {
// printf("%d ", i + 1);
// }
// }
//}
// Print function to update
void Print(char arr[ROWS][COLS], int x, int y)
{
int i = 0;
for (i = 0; i <= y ; i++)
{
printf("%d ", i);
}
printf("\n");
// Be careful 1 1 yes mine The beginning of 0 Location
for (i = 1; i <= x; i++) // Note that 1-x
{
printf("%d ", i );
int j = 0;
for (j = 1; j <= y; j++) // Note that 1-y
{
printf("%c ", arr[i][j]);
// Print is 9*9 Array :1-9 Is what you actually want to print ( In fact, that is 1-row 1-col)
}
printf("\n");
}
}
// Arrange thunder
void set_mine(char mine[ROWS][COLS], int row, int col)
{
int count = EASY_COUNT;
while (count)
{
// Generate random number
//int x = rand() % 9 + 1;//1-9 Subscript
//int y = rand() % 9 + 1;
// Optimize random numbers : To be in 1-row 1-col Lay thunder inside
int x = rand() % row + 1; // Pay attention to the subscript :1-row
int y = rand() % col + 1;
if (mine[x+1][y+1] == '0')// Note that the subscript must be added 1 Draw pictures to see
{
mine[x+1][y+1] = '1';
//EASY_COUNT--; // Lvalues are variables
count--;
}
}
}
// Mine clearance
int find_mine(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col)
{
int x = 0;
int y = 0;
printf(" Please enter the location you want to see :\n");
scanf("%d %d", &x, &y);
// Judge mine The number of mines
if ((x >= 1) && (x <= row) && (y >= 1) && (y <= col))
{
if ('1' == mine[x][y])
{
show[x][y] = '1';
return 9;// Because the number of surrounding mines is impossible 9
}
else
{
// Find the number of thunder around
char num = '0';
int i = 0;
for (i = (x - 1); i <= (x + 1); i++)
{
int j = 0;
for (j = (y - 1); j <= (y + 1); j++)
{
num += mine[i][j];
}
}
// Here should be
show[x][y] = num - 9 * '0';
return (num - 10 * '0');
to update : Another kind
//char num = mine[x - 1][y - 1] + mine[x - 1][y] + mine[x - 1][y + 1] +
// mine[x][y - 1] + mine[x][y + 1] +
// mine[x + 1][y - 1] + mine[x + 1][y] + mine[x + 1][y + 1];
//show[x][y] = num- 7 * '0'; // Be careful : Subtract from this ( Add the numbers -1)*‘0’ Because I have added ‘0’
//return (num - '0' * 8);
}
}
else
{
printf(" illegal input Please re-enter \n");
}
}3、game.h file
#pragma once
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#include<windows.h>
#define EASY_COUNT 10
#define ROW 9
#define COL 9
//#define ROWS 11
//#define COLS 11
// Optimize
#define ROWS (ROW +2)
#define COLS (COL +2)
// Initialize two arrays Optimize
void Init(char arr[ROWS][COLS], int rows, int cols, char set);
// Print function
void Print(char arr[ROW][COL], int x, int y);
// Arrange thunder
//void set_mine(char mine[ROWS][COLS], int rows, int cols);
void set_mine(char mine[ROWS][COLS], int row, int col); // modify
// Mine clearance
int find_mine(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col);
******************************* notes : This is a simple code No extended content
————————— All the anger of a person comes from the pain of his own incompetence .———————————
边栏推荐
- C. Diluc and Kaeya——Codeforces Round #724 (Div. 2)
- SQL programming task04 job - set operation
- Unique in Pimpl_ PTR compilation errors and Solutions
- [cmake command notes]find_ path
- Overview of visual object detection technology based on deep learning
- [Luogu] P2887 Sunscreen G
- 9. class and object practice and initialization list
- 165. cat climbing
- OOP multiple storage (class template)
- Autumn move script a
猜你喜欢

SQL programming task04 job - set operation
![Found several packages [runtime, main] in ‘/usr/local/Cellar/go/1.18/libexec/src/runtime;](/img/75/d2ad171d49611a6578faf2d390af29.jpg)
Found several packages [runtime, main] in ‘/usr/local/Cellar/go/1.18/libexec/src/runtime;

Cmake simple usage

office2016+visio2016

Template specialization template <>

5. explain function overloading

E-R diagram

Error reported when compiling basalt

Charles garbled code problem solving

Installing MySQL for Linux
随机推荐
Component development
[hdu] p1466 calculate the number of intersections of straight lines
Debian10 LVM logical volumes
Foundation Consolidation - Flex width is content width
Steps to implement a container global component
Uint8 serializing and deserializing pits using stringstream
There are animation characters interacting with each other when the mouse slides in the web page
[luogu] p1083 [noip2012 improvement group] borrow classroom (line segment tree)
C. Number of Pairs-Codeforces Round #725 (Div. 3)
Phantomjs Usage Summary
On function overloading from several examples
人民币的单位的大写
leetcode 91. Decode ways (medium)
Bc113 small leloding alarm clock
Data skew analysis of redis slice cluster
Get the direction of mouse movement
ERROR { err: YAMLException: end of the stream or a document separator is expected at line 6, colum
Pat class A - 1012 the best rank (PIT)
C serializabledictionary serialization / deserialization
Unique in Pimpl_ PTR compilation errors and Solutions