Tasks studies - laboratory
Project maintained by dawidolko Hosted on GitHub Pages — Theme by dawidolko
Lab06 - Tic-Tac-Toe Game
Tic-Tac-Toe Game https://drive.google.com/drive/folders/1-ceXITf1pQ7zAF0N5p8NqOXGnKtbVl4-?usp=sharing
Tasks
TicTacToe.exe
The current state of the game will be stored in a 2-dimensional array of characters. The state should be a global variable so that every function has access to it. In addition, information about the size of the board will be useful, which can also be stored in a global variable.
char tab[3][3] = { {' ', ' ', ' '},
{' ', ' ', ' '},
{' ', ' ', ' '} }
Step 1
Write a function renderboard() that will print the current state of the game to the console in the following form:
A B C
1
2
3
A B C
1 X O
2 X O
3 X
The function should be able to render a board with dimensions from 3x3 to 26x26 squares. Test the function. on boards:
Test boards
```c int const TAB_SIZE = 4; char board[TAB_SIZE][TAB_SIZE] = { {' ', ' ', ' ', ' '}, {' ', ' ', ' ', ' '}, {' ', ' ', ' ', ' '}, {' ', ' ', ' ', ' '} }; ``` ```c //Można zmienić rozmiar tablicy na mniejszy/więszy int const TAB_SIZE = 26; char board[TAB_SIZE][TAB_SIZE]; //w funkcji main ustawiane są wartośći w tablicy int main(int argc, char const *argv[]) { for (size_t i = 0; i < TAB_SIZE; i++) { for (size_t j = 0; j < TAB_SIZE; j++) { board[i][j] = ' '; } } return 0; } ```Hint
```c // The char type stores numbers from 0 to 255 which are decoded into characters visible in the console in the printf() function. // A - 65 // Z - 90 // a - 97 // z - 122 for(char i = 65; i<91; i++){ printf("%c ", i); } ``` Column headers can be printed before printing the board values. Row headers can be printed before iterating over the second dimension of the array.Solution:
The array can be initialized as a global variable, similarly to the size of the array:
int const TAB_SIZE = 3;
char board[3][3] = { {'X', 'O', 'O'},
{'X', 'X', 'O'},
{'O', ' ', 'X'} };
The size of the array must be known to us in order to efficiently iterate over subsequent values. The renderboard function can use global information about the size of the array:
renderboard(){
for(int i= 0; i< TAB_SIZE; i++){
for(int j= 0; j< TAB_SIZE; j++){
printf("%c", tab[i][j]);
}
}
}
Step 2
Write a function makeMove() that accepts a given player’s symbol on the board and returns the following codes:
- 0 - if the move is allowed (we occupy an empty square)
- 1 - if we go beyond the board area
- 2 - if we want to occupy another player’s square
- 3 - other errors In the body of the function, implement queries to the user to select the square they want to occupy.
Hint
Using the scanf() instruction, we can read several values at once. ```c char col; int row; printf("Enter the value of col and row, e.g. ", player); scanf(" %c %i", &col, &row); ``` After reading the values, just convert them to indexes (numbered from 0) and check the appropriate conditions.Step 3
Write a function score() that checks vertically, horizontally and diagonally whether one of the players has won, and if so, returns their symbol.
Hint
You should check whether there are the same signs in the columns, rows and diagonals. If you come across the ' ' sign, you can stop further searching in the column, row or diagonal. Similarly, if you come across another player's symbol.Step 4
Implement the game logic in the main function.
*AdvancedTicTacToe.exe
Modify the TicTacToe game so that the user can define the size of the board, the number and symbols of players.