联系方式

  • QQ:99515681
  • 邮箱:99515681@qq.com
  • 工作时间:8:00-21:00
  • 微信:codinghelp

您当前位置:首页 >> C/C++编程C/C++编程

日期:2021-07-26 10:16

Tic-tac-toe game in C.

Read the following task carefully before working on it. The tasks are

placed in such a way that they represent an obvious processing sequence. But you don't have to

keep the order, but some tasks can only be meaningfully processed after others.

At the end of the C part, a file should be created with which you can continue working in the Matlab

part

should. If you cannot create this file, you will find an example file for lateral entry

statistics.txt are provided, which you should work with in this case.

Requirements

? Apart from the struct, do not use any global variables.

? Adding your own functions is allowed and can be very helpful.

? The code should be structured and clear. Adhere to the standards you learned in the course.

? In places where errors can be expected, you should deal with them appropriately in your program.

C:

Task 1 (1 point)

Create a .c file and define a global constant TEXT_LENGTH with the value 776. You will need this

later for the rules of the game. In addition, the following libraries are needed which must be

integrated:

<stdio.h> <stdlib.h> <string.h> <time.h>

Also add the following sub-function to clear the input buffer.

void p u f f e r l e e r e n ( )

{

char nummer ;

do

{

nummer= getcha r ( ) ;

}

while ( nummer!= 1 0 ) ;

}

Task 2 (2 points)

Create a struct with the name account and the following variables as content:

? A string name. The name of the player is recorded in it, e.g. "Michael"

? An integer won_games. This records the number of games won by a user.

? An integer lost_games. This records the number of games a user has lost.

? An integer level. The level of the user is recorded in it.

Now create a field of size 10 of this struct as a global variable.

Task 3 (5 points)

Now write a sub-function

void read_instructions (char instructions [TEXT_LENGTH]).

This receives a string into which the text is to be read from the file. Now open in the sub-function

the file Instructions.txt and read the content in the string. Make sure that the program behaves

reliably in the event of an error.

Task 4 (3 points)

Now write a sub-function

int menu (int * accounts_amount, char instructions [TEXT_LENGTH], char game_table [9]).

The sub function must contain the pointer to the current number of accounts, the string which

contains the instructions and the character field of the playing field. In the sub-function you now

write a menu with nine points, in which you break down(分类,分段落) the functionality of the

program:

1. Show instructions: Output of the rules of the game read from the file.

2. Show accounts: All existing user accounts are displayed in a table.

3. Create account: A new account will be created.

4. Delete account: A desired account is deleted.

5. 1 Player (easy): A game against the computer is started (game mode: easy).

6. 1 Player (hard): A game against the computer is started (game mode: hard).

7. The statistics are carried out.

8. The program is closed and the accounts are saved.

Make sure that the program returns to the menu after each point and that any nonsensical entries are

intercepted.

Over time you will fill the individual menu items with your function calls.

Task 5 (1 point)

Now write a sub-function

void print_instructions (char instructions [TEXT_LENGTH])

The sub function takes a string contrary, which contains the rules of the game. Now output the rules

of the game on the console. Additionally you should still output the structure of the playing field

once. Use the following line of code for this:

p r i n t f ( " 0 | 1 | 2 \ n? ? ?\ n3 | 4 | 5 \ n? ? ?\ n6 | 7 | 8 \ n " ) ;

Task 6 (2 points)

Now write a sub-function

void create_account (int * accounts_amount)

This receives the pointer to the current number of accounts. Here the user should

enter his name. All other variables should be initialized with 0.

Task 7 (1 point)

Now write a sub-function

void print_accounts (int accounts_amount)

This receives the pointer to the current number of accounts. Now output all information of the

existing accounts in tabular form. The following should be present: Number, Name, Wins, Losses

and Level. When outputting, make sure that Number starts with 1 and not with 0.

Task 8 (3 points)

Now write a sub-function

void delete_account (int * accounts_amount)

This receives the pointer to the current number of accounts. Here the user should be asked which

account should be deleted. This should then be deleted.

Make sure that there are no gaps. In addition, any nonsensical inputs should be intercepted again.

Task 9 (7 points)

So that the account data is not lost after the program has ended, it should be saved at the end and

read in at the beginning of the program.

Use a binary file for this.

Write down two sub-functions read_accounts (int * accounts_amount) and save_accounts (int *

accounts_amount).

Both sub-functions receive the pointer to the current number of accounts. Make sure that the

program behaves reliably in the event of an error.

Note: It is better not only to save your struct in the binary file, but also the number of accounts

currently available. So you can also at the restart read in the number of accounts and then you know

exactly how many entries you have to read.

Task 10 (1 point)

Now create a function void show_game_table (char game_table [9]).

This receives the entries from the gaming table as a char-field and outputs them as a clear tic-tac-toe

field with dividing lines in the console.

Task 11 (1 point)

Now create a function void clear_table (char game_table [9]).

This receives the entries from the gaming table as a char-field and sets all entries from the last game

back to an empty space as an entry.

Task 12 (5 points)

Now create a function char check_win (char game_table [9]).

This receives the entries from the gaming table as a char-field and checks whether a winner can

already be determined with the previous entries or not. The function returns the winner's game

symbol (’x’ or ’o’). If neither player has won, a space is returned.

Task 13 (1 point)

Now create a function int random_v (long int * start_value).

This is given a start value as a pointer, which is initialized with the help of the time.h library. It

generated as return value a random value between 0 and 8, representing the 9 possible tic-tac-toe

fields.

Task14 (5 points)

There are several places in the program where a player or a bot should make a move.

Therefore encapsulate the moves in functions and always use them when a move is to be made.

? void player_move (char game_table [9], char sign)

This function includes reading in the desired position and intercepting faulty ones

Entering and writing the final, legal position on the board. To hand over

become the game board (game_table) and the player's sign, x or o (sign).

? void bot_move (char game_table [9], char sign, int hard, long int * start_value)

This function includes selecting the next legal position and writing it on the board. The game board

(game_table), the symbol of the bot, is handed over

(sign), an int (hard), which is either 1, if the bot is set to 'hard', or 0 if it is set to 'easy', as well as the

start value for the random function (start_value).

How exactly this move selection takes place in the two modes is explained in the following

exercises and therefore only gives points in the later exercises.

Task 15 (15 points)

Now create a function

void play_easy (int * accounts_amount, char game_table [9])

In this function, a game against a bot playing with the difficulty level ’easy’ is to be simulated in

single player mode. At the beginning of the function, all existing player accounts should be

displayed.

Then the single player can choose a player account which he would like to use to play the tic-tac-toe

game.

During the course of the game, the player (with the function player_move) and the bot (with the

function bot_move) take turns. For an overview of the playing field, this should be given once at the

beginning and then once after each move.

This output is not intended to be included in player_move or bot_move

After each move, the char check_win (char game_table [9]) function should be used to check

whether a winner has already been determined. If so, the game should be ended and the information

about who won the game should be displayed on the screen. In addition, the information whether

the single player has won or lost, are stored in their account. Finally the playing field should be reset

to an empty playing field with the help of the function:

void clear_table (char game_table [9]) can be reset to an empty playing field.

Note 1: The function int random_v (long int * start_value) only returns one playing field position. It

neither checks whether this position is still free nor does the function set the associated one

Game symbol ('x' or 'o') in the field.

Note 2: Make sure that all nonsensical entries and invalid entries on the field are intercepted.

Task 16 (16 points)

Now create a function

int policy (char game_table [9], char sign)

The function should use the transferring playing field and the transferring character (’x’ or ’o’) to

recognize whether another move can result in a winning situation on the playing field or not.

If a victory is possible by another move, the function returns the field position of the playing field

where the symbol leading to victory is to be placed as a return value. Can't handle a win situation

The function returns a negative value.

Task 17 (5 points)

Now create a function

void play_hard (int * accounts_amount, char game_table [9])

In this function, a game against a computer playing with the difficulty level ’hard’ is to be simulated

in single player mode. The function is identical to play_easy except for the transfer of the ’hard’

parameter to the bot_move function.

In contrast to the ’easy’ mode of the bot, this function checks in the ’hard’ mode with the help of the

int policy function whether your own victory is possible. If this is the case, the bot chooses this

move and the game is over. If the bot cannot win, it then checks whether the single player can win,

if yes, it prevents it. If neither a victory for the bot nor a victory for the single player is possible, the

bot randomly places its game symbol in a free space as before in the ’easy’ mode. This logic should

be completely encapsulated in the bot_move function.

Task18 (11 points)

With the functions have been written so far, you can play in single player mode ’easy’ and ’hard’, as

well as in two-player mode. A practical question now,

however, is: In which starting value give the best ways to win?

For this purpose, a sub-function void statistics (char game_table [9]) should be written, which

determines for each starting field firs_turn through 100 attempts whether a win was made with this

starting field.

Two computers should play against each other in the ’hard’ player mode. A victory is represented

with the value 1, a defeat with the value 0. The value pairs firs_turn and the

The result of the game should be for

first_turn = 0, 1, 2, ..., 8

can be output to a results file statistics.txt.

Please only write the numbers in the file, no text. Otherwise there will be problems with the

graphical representation of the values in Matlab.

Note 1: Create a field with 2 columns and 900 lines, whereby the value of the start field is always

saved in the first column and the result (0 or 1) is saved in the second column.

Note 2: If you always get the same results, you have probably done something wrong with the

transfer of the starting value.

Additional task (3 points)

As you have probably already noticed, our struct contains a variable called level, which has not

really been used up to this point. So now write a sub-function that classifies a level according to a

scheme and checks after each completed game whether a player has moved up or down a level.

There should be a total of 10 levels.

The ascent of a level should become more difficult as the level increases. This means that if moving

up from level 1 to level 2 requires 2 wins, for example, moving up from level 2 to 3 should

require at least 3 wins.

Matlab:

Task 19 (10 points)

Dependency: Task 18 or lateral entry with specifications

Read the text file you created in Exercise 18 into Matlab.

Make sure that you read the file in such a way that you can continue working with its content

accordingly.

Now plot the profit distribution as a bar chart in a plot window. Add a heading and axis names to

this. In addition, the x-axis of the plot should contain the numbers from 0-8, i.e. the starting field, as

distances and labels.

In addition, it should be determined which starting field is the best and this should be marked in the

plot.


相关文章

版权所有:编程辅导网 2021 All Rights Reserved 联系方式:QQ:99515681 微信:codinghelp 电子信箱:99515681@qq.com
免责声明:本站部分内容从网络整理而来,只供参考!如有版权问题可联系本站删除。 站长地图

python代写
微信客服:codinghelp