'Added main function with a code for testing a winner;'
This commit is contained in:
commit
4727ac78bd
120
src/tictactoe.c
Normal file
120
src/tictactoe.c
Normal file
@ -0,0 +1,120 @@
|
|||||||
|
/*
|
||||||
|
============================================================================
|
||||||
|
Name : tictactoe.c
|
||||||
|
Author :
|
||||||
|
Version :
|
||||||
|
Copyright : Your copyright notice
|
||||||
|
Description : Hello World in C, Ansi-style
|
||||||
|
============================================================================
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
/*
|
||||||
|
* 0 - nobody wins
|
||||||
|
* 1 - circles win
|
||||||
|
* 2 - crosses win
|
||||||
|
*/
|
||||||
|
int testForWin(int* board) {
|
||||||
|
// TODO make the code clean
|
||||||
|
|
||||||
|
// circles horizontal
|
||||||
|
if (board[0] == 1 && board[1] == 1 && board[2] == 1)
|
||||||
|
return 1; // circles in 1st line
|
||||||
|
if (board[3] == 1 && board[4] == 1 && board[5] == 1)
|
||||||
|
return 1; // circles in 2nd line
|
||||||
|
if (board[6] == 1 && board[7] == 1 && board[8] == 1)
|
||||||
|
return 1; // circles in 3rd line
|
||||||
|
|
||||||
|
// circle vertical
|
||||||
|
if (board[0] == 1 && board[3] == 1 && board[6] == 1)
|
||||||
|
return 1; // circles in 1st line
|
||||||
|
if (board[1] == 1 && board[4] == 1 && board[7] == 1)
|
||||||
|
return 1; // circles in 2nd line
|
||||||
|
if (board[2] == 1 && board[5] == 1 && board[8] == 1)
|
||||||
|
return 1; // circles in 3rd line
|
||||||
|
|
||||||
|
// circle diagonal
|
||||||
|
if (board[0] == 1 && board[4] == 1 && board[8] == 1)
|
||||||
|
return 1; // circles in 1st line
|
||||||
|
if (board[2] == 1 && board[4] == 1 && board[6] == 1)
|
||||||
|
return 1; // circles in 2nd line
|
||||||
|
|
||||||
|
// crosses horizontal
|
||||||
|
if (board[0] == 2 && board[1] == 2 && board[2] == 2)
|
||||||
|
return 2; // crosses in 1st line
|
||||||
|
if (board[3] == 2 && board[4] == 2 && board[5] == 2)
|
||||||
|
return 2; // crosses in 2nd line
|
||||||
|
if (board[6] == 2 && board[7] == 2 && board[8] == 2)
|
||||||
|
return 2; // crosses in 3rd line
|
||||||
|
|
||||||
|
// crosses vertical
|
||||||
|
if (board[0] == 2 && board[3] == 2 && board[6] == 2)
|
||||||
|
return 2; // crosses in 1st line
|
||||||
|
if (board[1] == 2 && board[4] == 2 && board[7] == 2)
|
||||||
|
return 2; // crosses in 2nd line
|
||||||
|
if (board[2] == 2 && board[5] == 2 && board[8] == 2)
|
||||||
|
return 2; // crosses in 3rd line
|
||||||
|
|
||||||
|
// crosses diagonal
|
||||||
|
if (board[0] == 2 && board[4] == 2 && board[8] == 2)
|
||||||
|
return 2; // crosses in 1st line
|
||||||
|
if (board[2] == 2 && board[4] == 2 && board[6] == 2)
|
||||||
|
return 2; // crosses in 2nd line
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
void printBoard(int* board) {
|
||||||
|
// TODO make this function smaller, and clean
|
||||||
|
printf("%c | %c | %c\n",
|
||||||
|
board[0] == 1 ? 'O' : (board[0] == 2 ? 'X' : ' '),
|
||||||
|
board[1] == 1 ? 'O' : (board[1] == 2 ? 'X' : ' '),
|
||||||
|
board[2] == 1 ? 'O' : (board[2] == 2 ? 'X' : ' '));
|
||||||
|
printf("------------\n");
|
||||||
|
printf("%c | %c | %c\n",
|
||||||
|
board[3] == 1 ? 'O' : (board[3] == 2 ? 'X' : ' '),
|
||||||
|
board[4] == 1 ? 'O' : (board[4] == 2 ? 'X' : ' '),
|
||||||
|
board[5] == 1 ? 'O' : (board[5] == 2 ? 'X' : ' '));
|
||||||
|
printf("------------\n");
|
||||||
|
printf("%c | %c | %c\n",
|
||||||
|
board[6] == 1 ? 'O' : (board[6] == 2 ? 'X' : ' '),
|
||||||
|
board[7] == 1 ? 'O' : (board[7] == 2 ? 'X' : ' '),
|
||||||
|
board[8] == 1 ? 'O' : (board[8] == 2 ? 'X' : ' '));
|
||||||
|
}
|
||||||
|
|
||||||
|
int main(void) {
|
||||||
|
int board[9];
|
||||||
|
for (int i = 0; i < 9; i++)
|
||||||
|
board[i] = 0;
|
||||||
|
|
||||||
|
board[0] = 1;
|
||||||
|
board[4] = 1;
|
||||||
|
board[8] = 1;
|
||||||
|
|
||||||
|
// clean board
|
||||||
|
printBoard(board);
|
||||||
|
|
||||||
|
// logic
|
||||||
|
while (1) {
|
||||||
|
// 1. TODO ask user for the move
|
||||||
|
// TODO validate the move (field has to be empty)
|
||||||
|
// TODO check if user wins, then print and exit
|
||||||
|
// the loop
|
||||||
|
|
||||||
|
// 2. TODO computer makes the move
|
||||||
|
|
||||||
|
// 3. TODO print the board
|
||||||
|
|
||||||
|
// 4. check if there is a winner, if there is a
|
||||||
|
// winner then exit the loop
|
||||||
|
int result = testForWin(board);
|
||||||
|
if (result != 0) {
|
||||||
|
printf("%s WINS!\n", result == 1 ? "circles" : "crosses");
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
puts("TicTacToe finished");
|
||||||
|
return EXIT_SUCCESS;
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user