From 491c8249671bd16655655219565eb0877d16e054 Mon Sep 17 00:00:00 2001 From: Aimee Reincke Date: Mon, 30 Jan 2023 10:35:25 +0100 Subject: [PATCH 01/28] added Tic Tac Toe files --- src/c/ticTacToe.c | 10 ++++++++++ src/c/ticTacToe.h | 6 ++++++ test/c/test_ticTacToe.c | 26 ++++++++++++++++++++++++++ 3 files changed, 42 insertions(+) create mode 100644 src/c/ticTacToe.c create mode 100644 src/c/ticTacToe.h create mode 100644 test/c/test_ticTacToe.c diff --git a/src/c/ticTacToe.c b/src/c/ticTacToe.c new file mode 100644 index 0000000..6d1d931 --- /dev/null +++ b/src/c/ticTacToe.c @@ -0,0 +1,10 @@ +#include +#include +#include + +#include "ticTacToe.h" + + +int something(int a){ + return a; +} \ No newline at end of file diff --git a/src/c/ticTacToe.h b/src/c/ticTacToe.h new file mode 100644 index 0000000..964ee03 --- /dev/null +++ b/src/c/ticTacToe.h @@ -0,0 +1,6 @@ +#ifndef TICTACTOE_H +#define TICTACTOE_H + +int something(int a); + +#endif \ No newline at end of file diff --git a/test/c/test_ticTacToe.c b/test/c/test_ticTacToe.c new file mode 100644 index 0000000..c76d3f9 --- /dev/null +++ b/test/c/test_ticTacToe.c @@ -0,0 +1,26 @@ +#ifdef TEST +#include "unity.h" +#include "ticTacToe.h" + +void setUp(void) +{ +} + +void tearDown(void) +{ +} + +void test_runExampleTest(void) +{ + /* arrange */ + int result; + int input = 1; + + /* act */ + result = something(input); + + /* assert */ + TEST_ASSERT_EQUAL_INT(1, result); +} + +#endif // TEST \ No newline at end of file From b818175f9981c04ba8305d63726dc8f788649fac Mon Sep 17 00:00:00 2001 From: Aimee Reincke Date: Mon, 30 Jan 2023 10:40:19 +0100 Subject: [PATCH 02/28] added print prompt function --- src/c/ticTacToe.c | 4 ++++ src/c/ticTacToe.h | 1 + 2 files changed, 5 insertions(+) diff --git a/src/c/ticTacToe.c b/src/c/ticTacToe.c index 6d1d931..3f7c43f 100644 --- a/src/c/ticTacToe.c +++ b/src/c/ticTacToe.c @@ -7,4 +7,8 @@ int something(int a){ return a; +} + +void printPrompt(){ + printf("Let's play Tic Tac Toe. You will use O, I will use X.\nJust enter the number of the field you choose.\nYou start.\n"); } \ No newline at end of file diff --git a/src/c/ticTacToe.h b/src/c/ticTacToe.h index 964ee03..7c268b6 100644 --- a/src/c/ticTacToe.h +++ b/src/c/ticTacToe.h @@ -2,5 +2,6 @@ #define TICTACTOE_H int something(int a); +void printPrompt(); #endif \ No newline at end of file From 714e1224bf2579c3a03b613629cfd84b9497507d Mon Sep 17 00:00:00 2001 From: Aimee Reincke Date: Mon, 30 Jan 2023 10:48:16 +0100 Subject: [PATCH 03/28] added print field function --- src/c/ticTacToe.c | 14 +++++++++++++- src/c/ticTacToe.h | 1 + 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/src/c/ticTacToe.c b/src/c/ticTacToe.c index 3f7c43f..b08dc32 100644 --- a/src/c/ticTacToe.c +++ b/src/c/ticTacToe.c @@ -10,5 +10,17 @@ int something(int a){ } void printPrompt(){ - printf("Let's play Tic Tac Toe. You will use O, I will use X.\nJust enter the number of the field you choose.\nYou start.\n"); + printf("Let's play Tic Tac Toe. You will use O, I will use X.\nJust enter the number of the field you choose as row col.\nYou start.\n"); +} + +void printField(char field[3][3]){ + for (int i = 0; i < 3; i++){ + for (int j = 0; j < 3; j++){ + printf("%d", field[i][j]); + if (j < 2) { + printf("|"); + } + } + printf("\n"); + } } \ No newline at end of file diff --git a/src/c/ticTacToe.h b/src/c/ticTacToe.h index 7c268b6..a01e4e4 100644 --- a/src/c/ticTacToe.h +++ b/src/c/ticTacToe.h @@ -3,5 +3,6 @@ int something(int a); void printPrompt(); +void printField(char field[3][3]); #endif \ No newline at end of file From 76453c39de84248ccc7c02efccb7504d7255e630 Mon Sep 17 00:00:00 2001 From: Aimee Reincke Date: Mon, 30 Jan 2023 10:55:04 +0100 Subject: [PATCH 04/28] added initiate Field function --- src/c/ticTacToe.c | 13 ++++++++++++- src/c/ticTacToe.h | 1 + 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/src/c/ticTacToe.c b/src/c/ticTacToe.c index b08dc32..b575e77 100644 --- a/src/c/ticTacToe.c +++ b/src/c/ticTacToe.c @@ -23,4 +23,15 @@ void printField(char field[3][3]){ } printf("\n"); } -} \ No newline at end of file +} + +char initField(){ + char field[3][3] = + { + {'-','-','-'}, + {'-','-','-'}, + {'-','-','-'} + }; + return field; +} + diff --git a/src/c/ticTacToe.h b/src/c/ticTacToe.h index a01e4e4..350fc78 100644 --- a/src/c/ticTacToe.h +++ b/src/c/ticTacToe.h @@ -4,5 +4,6 @@ int something(int a); void printPrompt(); void printField(char field[3][3]); +char initField(); #endif \ No newline at end of file From 764252dace8cf0fe1b4dfb71375d920124cb1baf Mon Sep 17 00:00:00 2001 From: Aimee Reincke Date: Mon, 30 Jan 2023 11:18:09 +0100 Subject: [PATCH 05/28] improved print and init field function --- src/c/ticTacToe.c | 22 +++++++++++++--------- src/c/ticTacToe.h | 3 ++- 2 files changed, 15 insertions(+), 10 deletions(-) diff --git a/src/c/ticTacToe.c b/src/c/ticTacToe.c index b575e77..c2347cb 100644 --- a/src/c/ticTacToe.c +++ b/src/c/ticTacToe.c @@ -16,7 +16,7 @@ void printPrompt(){ void printField(char field[3][3]){ for (int i = 0; i < 3; i++){ for (int j = 0; j < 3; j++){ - printf("%d", field[i][j]); + printf("%c", field[i][j]); if (j < 2) { printf("|"); } @@ -25,13 +25,17 @@ void printField(char field[3][3]){ } } -char initField(){ - char field[3][3] = - { - {'-','-','-'}, - {'-','-','-'}, - {'-','-','-'} - }; - return field; +void initField(char field[3][3]) { + for (int i = 0; i < 3; i++) { + for (int j = 0; j < 3; j++) { + field[i][j] = '-'; + } + } } +void getPlayerInput(char field[3][3]){ + int row, col; + printf("Enter the field as row col: "); + scanf("%d %d", &row, &col); + field[row][col] = 'O'; +} \ No newline at end of file diff --git a/src/c/ticTacToe.h b/src/c/ticTacToe.h index 350fc78..832972c 100644 --- a/src/c/ticTacToe.h +++ b/src/c/ticTacToe.h @@ -4,6 +4,7 @@ int something(int a); void printPrompt(); void printField(char field[3][3]); -char initField(); +void initField(char field[3][3]); +void getPlayerInput(char field[3][3]); #endif \ No newline at end of file From 2cbe1727f0eda0c3f413792623adcf6e846ec015 Mon Sep 17 00:00:00 2001 From: Aimee Reincke Date: Mon, 30 Jan 2023 13:46:00 +0100 Subject: [PATCH 06/28] valid user Input --- src/c/ticTacToe.c | 15 +++++++++++---- src/c/ticTacToe.h | 3 ++- test/c/test_ticTacToe.c | 11 ++++++----- 3 files changed, 19 insertions(+), 10 deletions(-) diff --git a/src/c/ticTacToe.c b/src/c/ticTacToe.c index c2347cb..5eab4a2 100644 --- a/src/c/ticTacToe.c +++ b/src/c/ticTacToe.c @@ -1,14 +1,11 @@ #include #include #include +#include #include "ticTacToe.h" -int something(int a){ - return a; -} - void printPrompt(){ printf("Let's play Tic Tac Toe. You will use O, I will use X.\nJust enter the number of the field you choose as row col.\nYou start.\n"); } @@ -37,5 +34,15 @@ void getPlayerInput(char field[3][3]){ int row, col; printf("Enter the field as row col: "); scanf("%d %d", &row, &col); + row -= 1; + col -= 1; field[row][col] = 'O'; +} + +bool validateUserInput(int row, int col){ + if (row < 3 && row >= 0){ + if (col < 3 && col >= 0){ + return true; + } + } } \ No newline at end of file diff --git a/src/c/ticTacToe.h b/src/c/ticTacToe.h index 832972c..250a462 100644 --- a/src/c/ticTacToe.h +++ b/src/c/ticTacToe.h @@ -1,10 +1,11 @@ #ifndef TICTACTOE_H #define TICTACTOE_H +#include -int something(int a); void printPrompt(); void printField(char field[3][3]); void initField(char field[3][3]); void getPlayerInput(char field[3][3]); +bool validateUserInput(int row, int col); #endif \ No newline at end of file diff --git a/test/c/test_ticTacToe.c b/test/c/test_ticTacToe.c index c76d3f9..d912a7a 100644 --- a/test/c/test_ticTacToe.c +++ b/test/c/test_ticTacToe.c @@ -1,6 +1,7 @@ #ifdef TEST #include "unity.h" #include "ticTacToe.h" +#include void setUp(void) { @@ -10,17 +11,17 @@ void tearDown(void) { } -void test_runExampleTest(void) +void test_ticTacToe_validUserInput(void) { /* arrange */ - int result; - int input = 1; + bool result; + int row = 2, col = 0; /* act */ - result = something(input); + result = validateUserInput(row, col); /* assert */ - TEST_ASSERT_EQUAL_INT(1, result); + TEST_ASSERT_EQUAL_INT(true, result); } #endif // TEST \ No newline at end of file From ac8629e1f8057c3ddc26753feea9f214ca46889a Mon Sep 17 00:00:00 2001 From: Aimee Reincke Date: Mon, 30 Jan 2023 13:47:41 +0100 Subject: [PATCH 07/28] invalid user Input --- src/c/ticTacToe.c | 1 + test/c/test_ticTacToe.c | 14 ++++++++++++++ 2 files changed, 15 insertions(+) diff --git a/src/c/ticTacToe.c b/src/c/ticTacToe.c index 5eab4a2..f8d5e1a 100644 --- a/src/c/ticTacToe.c +++ b/src/c/ticTacToe.c @@ -45,4 +45,5 @@ bool validateUserInput(int row, int col){ return true; } } + return false; } \ No newline at end of file diff --git a/test/c/test_ticTacToe.c b/test/c/test_ticTacToe.c index d912a7a..bb0509c 100644 --- a/test/c/test_ticTacToe.c +++ b/test/c/test_ticTacToe.c @@ -24,4 +24,18 @@ void test_ticTacToe_validUserInput(void) TEST_ASSERT_EQUAL_INT(true, result); } +void test_ticTacToe_invalidUserInput(void) +{ + /* arrange */ + bool result; + int row = 1, col = 3; + + /* act */ + result = validateUserInput(row, col); + + /* assert */ + TEST_ASSERT_EQUAL_INT(false, result); +} + + #endif // TEST \ No newline at end of file From 050adc24c1925008eddaf5277fdf015ca7427e6c Mon Sep 17 00:00:00 2001 From: Aimee Reincke Date: Mon, 30 Jan 2023 13:52:00 +0100 Subject: [PATCH 08/28] refactoring: implemented validate input --- src/c/ticTacToe.c | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/src/c/ticTacToe.c b/src/c/ticTacToe.c index f8d5e1a..38be611 100644 --- a/src/c/ticTacToe.c +++ b/src/c/ticTacToe.c @@ -32,11 +32,17 @@ void initField(char field[3][3]) { void getPlayerInput(char field[3][3]){ int row, col; - printf("Enter the field as row col: "); - scanf("%d %d", &row, &col); - row -= 1; - col -= 1; - field[row][col] = 'O'; + bool valid = false; + while (!valid){ + printf("Enter the field as row col: "); + scanf("%d %d", &row, &col); + row -= 1; + col -= 1; + if(validateUserInput(row, col)){ + field[row][col] = 'O'; + valid = validateUserInput(row, col); + } + } } bool validateUserInput(int row, int col){ From 4333fbf343ae5e4c939ff8f877babd4feb6fbb79 Mon Sep 17 00:00:00 2001 From: Aimee Reincke Date: Wed, 1 Feb 2023 15:17:00 +0100 Subject: [PATCH 09/28] Player wins Game --- src/c/ticTacToe.c | 35 +++++++++++++++++++++++++++++++++++ src/c/ticTacToe.h | 8 ++++++++ test/c/test_ticTacToe.c | 17 +++++++++++++++++ 3 files changed, 60 insertions(+) diff --git a/src/c/ticTacToe.c b/src/c/ticTacToe.c index 38be611..a4208a8 100644 --- a/src/c/ticTacToe.c +++ b/src/c/ticTacToe.c @@ -52,4 +52,39 @@ bool validateUserInput(int row, int col){ } } return false; +} + +int wasGameWon(char field[3][3]){ + int winner = NOWINNERYET; + if(field[0][0] == 'O'){ + if (field[0][0] == field[0][1] && field[0][2] == field[0][1]){ + winner = PLAYER; + } + else if (field[0][0] == field[1][0] && field[1][0] == field[2][0]){ + winner = PLAYER; + } + else if (field[0][0] == field[1][1] && field[1][1] == field[2][2]){ + winner = PLAYER; + } + } + else if(field[2][2] == 'O'){ + if (field[2][2] == field[2][1] && field[2][2] == field[2][0]){ + winner = PLAYER; + } + else if (field[2][2] == field[1][2] && field[1][2] == field[0][2]){ + winner = PLAYER; + } + } + if(field[1][1] == 'O'){ + if (field[1][1] == field[0][1] && field[0][1] == field[2][1]){ + winner = PLAYER; + } + else if (field[1][1] == field[1][0] && field[1][0] == field[1][2]){ + winner = PLAYER; + } + else if (field[2][0] == field[1][1] && field[1][1] == field[0][2]){ + winner = PLAYER; + } + } + return winner; } \ No newline at end of file diff --git a/src/c/ticTacToe.h b/src/c/ticTacToe.h index 250a462..9bca53a 100644 --- a/src/c/ticTacToe.h +++ b/src/c/ticTacToe.h @@ -2,10 +2,18 @@ #define TICTACTOE_H #include +enum Winner{ + PLAYER, + COMPUTER, + NOWINNERYET, + DRAW +}; + void printPrompt(); void printField(char field[3][3]); void initField(char field[3][3]); void getPlayerInput(char field[3][3]); bool validateUserInput(int row, int col); +int wasGameWon(char field[3][3]); #endif \ No newline at end of file diff --git a/test/c/test_ticTacToe.c b/test/c/test_ticTacToe.c index bb0509c..f86583b 100644 --- a/test/c/test_ticTacToe.c +++ b/test/c/test_ticTacToe.c @@ -37,5 +37,22 @@ void test_ticTacToe_invalidUserInput(void) TEST_ASSERT_EQUAL_INT(false, result); } +void test_ticTacToe_playerWins(void) +{ + /* arrange */ + int result; + char field[][3] = { + {'O', 'O', '-'}, + {'-', 'O', '-'}, + {'-', '-', 'O'} + }; + + /* act */ + result = wasGameWon(field); + + /* assert */ + TEST_ASSERT_EQUAL_INT(PLAYER, result); +} + #endif // TEST \ No newline at end of file From f0a9f0fddefd33a5248ccc4117f1c176a676b3bf Mon Sep 17 00:00:00 2001 From: Aimee Reincke Date: Wed, 1 Feb 2023 15:19:44 +0100 Subject: [PATCH 10/28] computer wins game --- src/c/ticTacToe.c | 30 ++++++++++++++++++++++++++++++ test/c/test_ticTacToe.c | 16 ++++++++++++++++ 2 files changed, 46 insertions(+) diff --git a/src/c/ticTacToe.c b/src/c/ticTacToe.c index a4208a8..f5ecc78 100644 --- a/src/c/ticTacToe.c +++ b/src/c/ticTacToe.c @@ -86,5 +86,35 @@ int wasGameWon(char field[3][3]){ winner = PLAYER; } } + if(field[0][0] == 'X'){ + if (field[0][0] == field[0][1] && field[0][2] == field[0][1]){ + winner = COMPUTER; + } + else if (field[0][0] == field[1][0] && field[1][0] == field[2][0]){ + winner = COMPUTER; + } + else if (field[0][0] == field[1][1] && field[1][1] == field[2][2]){ + winner = COMPUTER; + } + } + else if(field[2][2] == 'X'){ + if (field[2][2] == field[2][1] && field[2][2] == field[2][0]){ + winner = COMPUTER; + } + else if (field[2][2] == field[1][2] && field[1][2] == field[0][2]){ + winner = COMPUTER; + } + } + if(field[1][1] == 'X'){ + if (field[1][1] == field[0][1] && field[0][1] == field[2][1]){ + winner = COMPUTER; + } + else if (field[1][1] == field[1][0] && field[1][0] == field[1][2]){ + winner = COMPUTER; + } + else if (field[2][0] == field[1][1] && field[1][1] == field[0][2]){ + winner = COMPUTER; + } + } return winner; } \ No newline at end of file diff --git a/test/c/test_ticTacToe.c b/test/c/test_ticTacToe.c index f86583b..ae35d69 100644 --- a/test/c/test_ticTacToe.c +++ b/test/c/test_ticTacToe.c @@ -54,5 +54,21 @@ void test_ticTacToe_playerWins(void) TEST_ASSERT_EQUAL_INT(PLAYER, result); } +void test_ticTacToe_ComputerWins(void) +{ + /* arrange */ + int result; + char field[][3] = { + {'X', '-', '-'}, + {'X', '-', '-'}, + {'X', '-', '-'} + }; + + /* act */ + result = wasGameWon(field); + + /* assert */ + TEST_ASSERT_EQUAL_INT(COMPUTER, result); +} #endif // TEST \ No newline at end of file From 51818122b7326b23511857e69cb8b0dea32bed13 Mon Sep 17 00:00:00 2001 From: Aimee Reincke Date: Wed, 1 Feb 2023 15:30:29 +0100 Subject: [PATCH 11/28] Field full no winner --- src/c/ticTacToe.c | 17 +++++++++++++---- test/c/test_ticTacToe.c | 17 +++++++++++++++++ 2 files changed, 30 insertions(+), 4 deletions(-) diff --git a/src/c/ticTacToe.c b/src/c/ticTacToe.c index f5ecc78..985549d 100644 --- a/src/c/ticTacToe.c +++ b/src/c/ticTacToe.c @@ -55,7 +55,16 @@ bool validateUserInput(int row, int col){ } int wasGameWon(char field[3][3]){ - int winner = NOWINNERYET; + int winner = NOWINNERYET; + bool fieldFull = true; + for (int i = 0; i < 3; i++){ + for (int j = 0; j < 3; j++){ + if(field[i][j]=='-'){ + fieldFull = false; + } + } + } + if (fieldFull == true) winner = DRAW; if(field[0][0] == 'O'){ if (field[0][0] == field[0][1] && field[0][2] == field[0][1]){ winner = PLAYER; @@ -75,7 +84,7 @@ int wasGameWon(char field[3][3]){ winner = PLAYER; } } - if(field[1][1] == 'O'){ + else if(field[1][1] == 'O'){ if (field[1][1] == field[0][1] && field[0][1] == field[2][1]){ winner = PLAYER; } @@ -86,7 +95,7 @@ int wasGameWon(char field[3][3]){ winner = PLAYER; } } - if(field[0][0] == 'X'){ + else if(field[0][0] == 'X'){ if (field[0][0] == field[0][1] && field[0][2] == field[0][1]){ winner = COMPUTER; } @@ -105,7 +114,7 @@ int wasGameWon(char field[3][3]){ winner = COMPUTER; } } - if(field[1][1] == 'X'){ + else if(field[1][1] == 'X'){ if (field[1][1] == field[0][1] && field[0][1] == field[2][1]){ winner = COMPUTER; } diff --git a/test/c/test_ticTacToe.c b/test/c/test_ticTacToe.c index ae35d69..bc4439f 100644 --- a/test/c/test_ticTacToe.c +++ b/test/c/test_ticTacToe.c @@ -71,4 +71,21 @@ void test_ticTacToe_ComputerWins(void) TEST_ASSERT_EQUAL_INT(COMPUTER, result); } +void test_ticTacToe_NooneWins(void) +{ + /* arrange */ + int result; + char field[][3] = { + {'X', 'O', 'O'}, + {'O', 'X', 'X'}, + {'X', 'O', 'O'} + }; + + /* act */ + result = wasGameWon(field); + + /* assert */ + TEST_ASSERT_EQUAL_INT(DRAW, result); +} + #endif // TEST \ No newline at end of file From 706f6b31bc05e0e5be1f5078b1d3fa8c47b8945a Mon Sep 17 00:00:00 2001 From: Aimee Reincke Date: Wed, 1 Feb 2023 15:33:27 +0100 Subject: [PATCH 12/28] test for no winner yet --- src/c/ticTacToe.c | 30 +++++++++++++++--------------- test/c/test_ticTacToe.c | 17 +++++++++++++++++ 2 files changed, 32 insertions(+), 15 deletions(-) diff --git a/src/c/ticTacToe.c b/src/c/ticTacToe.c index 985549d..8d0d1fb 100644 --- a/src/c/ticTacToe.c +++ b/src/c/ticTacToe.c @@ -69,59 +69,59 @@ int wasGameWon(char field[3][3]){ if (field[0][0] == field[0][1] && field[0][2] == field[0][1]){ winner = PLAYER; } - else if (field[0][0] == field[1][0] && field[1][0] == field[2][0]){ + if (field[0][0] == field[1][0] && field[1][0] == field[2][0]){ winner = PLAYER; } - else if (field[0][0] == field[1][1] && field[1][1] == field[2][2]){ + if (field[0][0] == field[1][1] && field[1][1] == field[2][2]){ winner = PLAYER; } } - else if(field[2][2] == 'O'){ + if(field[2][2] == 'O'){ if (field[2][2] == field[2][1] && field[2][2] == field[2][0]){ winner = PLAYER; } - else if (field[2][2] == field[1][2] && field[1][2] == field[0][2]){ + if (field[2][2] == field[1][2] && field[1][2] == field[0][2]){ winner = PLAYER; } } - else if(field[1][1] == 'O'){ + if(field[1][1] == 'O'){ if (field[1][1] == field[0][1] && field[0][1] == field[2][1]){ winner = PLAYER; } - else if (field[1][1] == field[1][0] && field[1][0] == field[1][2]){ + if (field[1][1] == field[1][0] && field[1][0] == field[1][2]){ winner = PLAYER; } - else if (field[2][0] == field[1][1] && field[1][1] == field[0][2]){ + if (field[2][0] == field[1][1] && field[1][1] == field[0][2]){ winner = PLAYER; } } - else if(field[0][0] == 'X'){ + if(field[0][0] == 'X'){ if (field[0][0] == field[0][1] && field[0][2] == field[0][1]){ winner = COMPUTER; } - else if (field[0][0] == field[1][0] && field[1][0] == field[2][0]){ + if (field[0][0] == field[1][0] && field[1][0] == field[2][0]){ winner = COMPUTER; } - else if (field[0][0] == field[1][1] && field[1][1] == field[2][2]){ + if (field[0][0] == field[1][1] && field[1][1] == field[2][2]){ winner = COMPUTER; } } - else if(field[2][2] == 'X'){ + if(field[2][2] == 'X'){ if (field[2][2] == field[2][1] && field[2][2] == field[2][0]){ winner = COMPUTER; } - else if (field[2][2] == field[1][2] && field[1][2] == field[0][2]){ + if (field[2][2] == field[1][2] && field[1][2] == field[0][2]){ winner = COMPUTER; } } - else if(field[1][1] == 'X'){ + if(field[1][1] == 'X'){ if (field[1][1] == field[0][1] && field[0][1] == field[2][1]){ winner = COMPUTER; } - else if (field[1][1] == field[1][0] && field[1][0] == field[1][2]){ + if (field[1][1] == field[1][0] && field[1][0] == field[1][2]){ winner = COMPUTER; } - else if (field[2][0] == field[1][1] && field[1][1] == field[0][2]){ + if (field[2][0] == field[1][1] && field[1][1] == field[0][2]){ winner = COMPUTER; } } diff --git a/test/c/test_ticTacToe.c b/test/c/test_ticTacToe.c index bc4439f..69870d1 100644 --- a/test/c/test_ticTacToe.c +++ b/test/c/test_ticTacToe.c @@ -88,4 +88,21 @@ void test_ticTacToe_NooneWins(void) TEST_ASSERT_EQUAL_INT(DRAW, result); } +void test_ticTacToe_NoWinnerYet(void) +{ + /* arrange */ + int result; + char field[][3] = { + {'X', 'O', 'O'}, + {'O', '-', 'X'}, + {'X', 'O', 'O'} + }; + + /* act */ + result = wasGameWon(field); + + /* assert */ + TEST_ASSERT_EQUAL_INT(NOWINNERYET, result); +} + #endif // TEST \ No newline at end of file From 3887b527d7b3f17b5a0b17af24be6899e1fbbf77 Mon Sep 17 00:00:00 2001 From: Aimee Reincke Date: Thu, 2 Feb 2023 15:41:48 +0100 Subject: [PATCH 13/28] refactoring: less duplication --- src/c/ticTacToe.c | 92 ++++++++++++++++++++++++++--------------------- 1 file changed, 51 insertions(+), 41 deletions(-) diff --git a/src/c/ticTacToe.c b/src/c/ticTacToe.c index 8d0d1fb..3cc5280 100644 --- a/src/c/ticTacToe.c +++ b/src/c/ticTacToe.c @@ -65,64 +65,74 @@ int wasGameWon(char field[3][3]){ } } if (fieldFull == true) winner = DRAW; - if(field[0][0] == 'O'){ + if(field[0][0] == 'O' || field[0][0] == 'X'){ if (field[0][0] == field[0][1] && field[0][2] == field[0][1]){ - winner = PLAYER; + if (field[0][0] == 'O'){ + winner = PLAYER; + } + else if (field[0][0] == 'X'){ + winner = COMPUTER; + } } if (field[0][0] == field[1][0] && field[1][0] == field[2][0]){ - winner = PLAYER; + if (field[0][0] == 'O'){ + winner = PLAYER; + } + else if (field[0][0] == 'X'){ + winner = COMPUTER; + } } if (field[0][0] == field[1][1] && field[1][1] == field[2][2]){ - winner = PLAYER; + if (field[0][0] == 'O'){ + winner = PLAYER; + } + else if (field[0][0] == 'X'){ + winner = COMPUTER; + } } } - if(field[2][2] == 'O'){ + if(field[2][2] == 'O' || field[2][2] == 'X'){ if (field[2][2] == field[2][1] && field[2][2] == field[2][0]){ - winner = PLAYER; + if (field[2][2] == 'O'){ + winner = PLAYER; + } + else if (field[2][2] == 'X'){ + winner = COMPUTER; + } } if (field[2][2] == field[1][2] && field[1][2] == field[0][2]){ - winner = PLAYER; + if (field[2][2] == 'O'){ + winner = PLAYER; + } + else if (field[2][2] == 'X'){ + winner = COMPUTER; + } } } - if(field[1][1] == 'O'){ + if(field[1][1] == 'O' || field[1][1] == 'X'){ if (field[1][1] == field[0][1] && field[0][1] == field[2][1]){ - winner = PLAYER; + if (field[1][1] == 'O'){ + winner = PLAYER; + } + else if (field[1][1] == 'X'){ + winner = COMPUTER; + } } if (field[1][1] == field[1][0] && field[1][0] == field[1][2]){ - winner = PLAYER; - } - if (field[2][0] == field[1][1] && field[1][1] == field[0][2]){ - winner = PLAYER; - } - } - if(field[0][0] == 'X'){ - if (field[0][0] == field[0][1] && field[0][2] == field[0][1]){ - winner = COMPUTER; - } - if (field[0][0] == field[1][0] && field[1][0] == field[2][0]){ - winner = COMPUTER; - } - if (field[0][0] == field[1][1] && field[1][1] == field[2][2]){ - winner = COMPUTER; - } - } - if(field[2][2] == 'X'){ - if (field[2][2] == field[2][1] && field[2][2] == field[2][0]){ - winner = COMPUTER; - } - if (field[2][2] == field[1][2] && field[1][2] == field[0][2]){ - winner = COMPUTER; - } - } - if(field[1][1] == 'X'){ - if (field[1][1] == field[0][1] && field[0][1] == field[2][1]){ - winner = COMPUTER; - } - if (field[1][1] == field[1][0] && field[1][0] == field[1][2]){ - winner = COMPUTER; + if (field[1][1] == 'O'){ + winner = PLAYER; + } + else if (field[1][1] == 'X'){ + winner = COMPUTER; + } } if (field[2][0] == field[1][1] && field[1][1] == field[0][2]){ - winner = COMPUTER; + if (field[1][1] == 'O'){ + winner = PLAYER; + } + else if (field[1][1] == 'X'){ + winner = COMPUTER; + } } } return winner; From de66f5ef8d2d0a4b3c6e846e47da499d6a84778a Mon Sep 17 00:00:00 2001 From: Aimee Reincke Date: Thu, 2 Feb 2023 15:49:12 +0100 Subject: [PATCH 14/28] refactoring: more cohesive naming --- src/c/ticTacToe.c | 9 +++++---- src/c/ticTacToe.h | 2 +- test/c/test_ticTacToe.c | 4 ++-- 3 files changed, 8 insertions(+), 7 deletions(-) diff --git a/src/c/ticTacToe.c b/src/c/ticTacToe.c index 3cc5280..7d2105f 100644 --- a/src/c/ticTacToe.c +++ b/src/c/ticTacToe.c @@ -38,14 +38,14 @@ void getPlayerInput(char field[3][3]){ scanf("%d %d", &row, &col); row -= 1; col -= 1; - if(validateUserInput(row, col)){ + if(validatePlayerInput(row, col)){ field[row][col] = 'O'; - valid = validateUserInput(row, col); + valid = validatePlayerInput(row, col); } } } -bool validateUserInput(int row, int col){ +bool validatePlayerInput(int row, int col){ if (row < 3 && row >= 0){ if (col < 3 && col >= 0){ return true; @@ -136,4 +136,5 @@ int wasGameWon(char field[3][3]){ } } return winner; -} \ No newline at end of file +} + diff --git a/src/c/ticTacToe.h b/src/c/ticTacToe.h index 9bca53a..ec68f18 100644 --- a/src/c/ticTacToe.h +++ b/src/c/ticTacToe.h @@ -13,7 +13,7 @@ void printPrompt(); void printField(char field[3][3]); void initField(char field[3][3]); void getPlayerInput(char field[3][3]); -bool validateUserInput(int row, int col); +bool validatePlayerInput(int row, int col); int wasGameWon(char field[3][3]); #endif \ No newline at end of file diff --git a/test/c/test_ticTacToe.c b/test/c/test_ticTacToe.c index 69870d1..140ce7a 100644 --- a/test/c/test_ticTacToe.c +++ b/test/c/test_ticTacToe.c @@ -18,7 +18,7 @@ void test_ticTacToe_validUserInput(void) int row = 2, col = 0; /* act */ - result = validateUserInput(row, col); + result = validatePlayerInput(row, col); /* assert */ TEST_ASSERT_EQUAL_INT(true, result); @@ -31,7 +31,7 @@ void test_ticTacToe_invalidUserInput(void) int row = 1, col = 3; /* act */ - result = validateUserInput(row, col); + result = validatePlayerInput(row, col); /* assert */ TEST_ASSERT_EQUAL_INT(false, result); From d95ca2b2e9ae4828b6429db856b94fb57961bb71 Mon Sep 17 00:00:00 2001 From: Aimee Reincke Date: Thu, 2 Feb 2023 16:20:44 +0100 Subject: [PATCH 15/28] added get computer input --- src/c/ticTacToe.c | 12 ++++++++++++ src/c/ticTacToe.h | 1 + 2 files changed, 13 insertions(+) diff --git a/src/c/ticTacToe.c b/src/c/ticTacToe.c index 7d2105f..3f3066a 100644 --- a/src/c/ticTacToe.c +++ b/src/c/ticTacToe.c @@ -138,3 +138,15 @@ int wasGameWon(char field[3][3]){ return winner; } +void getComputerInput(char field[3][3]){ + bool symbolSet = false; + while (!symbolSet){ + int row, col; + row = rand() % 3; + col = rand() % 3; + if (field[row][col] == '-'){ + field[row][col] = 'X'; + symbolSet = true; + } + } +} diff --git a/src/c/ticTacToe.h b/src/c/ticTacToe.h index ec68f18..222383d 100644 --- a/src/c/ticTacToe.h +++ b/src/c/ticTacToe.h @@ -15,5 +15,6 @@ void initField(char field[3][3]); void getPlayerInput(char field[3][3]); bool validatePlayerInput(int row, int col); int wasGameWon(char field[3][3]); +void getComputerInput(char field[3][3]); #endif \ No newline at end of file From 21269448d319cafa47109cd2a1777ad91e4f6dca Mon Sep 17 00:00:00 2001 From: Aimee Reincke Date: Thu, 2 Feb 2023 17:41:32 +0100 Subject: [PATCH 16/28] added fillAlmostFull for computerwin --- src/c/ticTacToe.c | 265 +++++++++++++++++++++++++++++++++++++++++++++- src/c/ticTacToe.h | 1 + 2 files changed, 265 insertions(+), 1 deletion(-) diff --git a/src/c/ticTacToe.c b/src/c/ticTacToe.c index 3f3066a..f811deb 100644 --- a/src/c/ticTacToe.c +++ b/src/c/ticTacToe.c @@ -139,7 +139,8 @@ int wasGameWon(char field[3][3]){ } void getComputerInput(char field[3][3]){ - bool symbolSet = false; + bool symbolSet = false, *pt = &symbolSet; + fillAlmostFull(field, pt); while (!symbolSet){ int row, col; row = rand() % 3; @@ -150,3 +151,265 @@ void getComputerInput(char field[3][3]){ } } } + + +void fillAlmostFull(char field[3][3], bool* pt) { + if (field[0][0] == 'X') { + if (field[0][1] == 'X') { + field[0][2] = 'X'; + *pt = true; + return; + } + else if (field[0][2] == 'X') { + field[0][1] = 'X'; + *pt = true; + return; + } + else if (field[1][0] == 'X') { + field[2][0] = 'X'; + *pt = true; + return; + } + else if (field[2][0] == 'X') { + field[1][0] = 'X'; + *pt = true; + return; + } + else if (field[1][1] == 'X') { + field[2][2] = 'X'; + *pt = true; + return; + } + else if (field[2][2] == 'X') { + field[1][1] = 'X'; + *pt = true; + return; + } + } + if (field[0][1] == 'X') { + if (field[0][0] == 'X') { + field[0][2] = 'X'; + *pt = true; + return; + } + else if (field[0][2] == 'X') { + field[0][0] = 'X'; + *pt = true; + return; + } + else if (field[1][1] == 'X') { + field[2][1] = 'X'; + *pt = true; + return; + } + else if (field[2][1] == 'X') { + field[1][1] = 'X'; + *pt = true; + return; + } + } + if (field[0][2] == 'X') { + if (field[0][0] == 'X') { + field[0][1] = 'X'; + *pt = true; + return; + } + else if (field[0][1] == 'X') { + field[0][0] = 'X'; + *pt = true; + return; + } + else if (field[1][1] == 'X') { + field[2][0] = 'X'; + *pt = true; + return; + } + else if (field[2][0] == 'X') { + field[1][1] = 'X'; + *pt = true; + return; + } + else if (field[1][2] == 'X') { + field[2][2] = 'X'; + *pt = true; + return; + } + else if (field[2][2] == 'X') { + field[1][2] = 'X'; + *pt = true; + return; + } + } + if (field[1][0] == 'X') { + if (field[1][1] == 'X') { + field[1][2] = 'X'; + *pt = true; + return; + } + else if (field[1][2] == 'X') { + field[1][1] = 'X'; + *pt = true; + return; + } + else if (field[0][0] == 'X') { + field[2][0] = 'X'; + *pt = true; + return; + } + else if (field[2][0] == 'X') { + field[0][0] = 'X'; + *pt = true; + return; + } + } + if (field[1][1] == 'X') { + if (field[1][0] == 'X') { + field[1][2] = 'X'; + *pt = true; + return; + } + else if (field[1][2] == 'X') { + field[1][0] = 'X'; + *pt = true; + return; + } + else if (field[0][1] == 'X') { + field[2][1] = 'X'; + *pt = true; + return; + } + else if (field[2][1] == 'X') { + field[0][1] = 'X'; + *pt = true; + return; + } + else if (field[0][0] == 'X') { + field[2][2] = 'X'; + *pt = true; + return; + } + else if (field[2][2] == 'X') { + field[0][0] = 'X'; + *pt = true; + return; + } + else if (field[0][2] == 'X') { + field[2][0] = 'X'; + *pt = true; + return; + } + else if (field[2][0] == 'X') { + field[0][2] = 'X'; + *pt = true; + return; + } + } + if (field[1][2] == 'X') { + if (field[1][0] == 'X') { + field[1][1] = 'X'; + *pt = true; + return; + } + else if (field[1][1] == 'X') { + field[1][0] = 'X'; + *pt = true; + return; + } + else if (field[0][2] == 'X') { + field[2][2] = 'X'; + *pt = true; + return; + } + else if (field[2][2] == 'X') { + field[0][2] = 'X'; + *pt = true; + return; + } + } + if (field[2][0] == 'X') { + if (field[2][1] == 'X') { + field[2][2] = 'X'; + *pt = true; + return; + } + else if (field[2][2] == 'X') { + field[2][1] = 'X'; + *pt = true; + return; + } + else if (field[0][0] == 'X') { + field[1][0] = 'X'; + *pt = true; + return; + } + else if (field[1][0] == 'X') { + field[0][0] = 'X'; + *pt = true; + return; + } + else if (field[1][1] == 'X') { + field[0][2] = 'X'; + *pt = true; + return; + } + else if (field[0][2] == 'X') { + field[1][1] = 'X'; + *pt = true; + return; + } + } + if (field[2][1] == 'X') { + if (field[2][0] == 'X') { + field[2][2] = 'X'; + *pt = true; + return; + } + else if (field[2][2] == 'X') { + field[2][0] = 'X'; + *pt = true; + return; + } + else if (field[1][1] == 'X') { + field[0][1] = 'X'; + *pt = true; + return; + } + else if (field[0][1] == 'X') { + field[1][1] = 'X'; + *pt = true; + return; + } + } + if (field[2][2] == 'X') { + if (field[2][0] == 'X') { + field[2][1] = 'X'; + *pt = true; + return; + } + else if (field[2][1] == 'X') { + field[2][0] = 'X'; + *pt = true; + return; + } + else if (field[0][2] == 'X') { + field[1][2] = 'X'; + *pt = true; + return; + } + else if (field[1][2] == 'X') { + field[0][2] = 'X'; + *pt = true; + return; + } + else if (field[1][1] == 'X') { + field[0][0] = 'X'; + *pt = true; + return; + } + else if (field[0][0] == 'X') { + field[1][1] = 'X'; + *pt = true; + return; + } + } +} diff --git a/src/c/ticTacToe.h b/src/c/ticTacToe.h index 222383d..6db536c 100644 --- a/src/c/ticTacToe.h +++ b/src/c/ticTacToe.h @@ -16,5 +16,6 @@ void getPlayerInput(char field[3][3]); bool validatePlayerInput(int row, int col); int wasGameWon(char field[3][3]); void getComputerInput(char field[3][3]); +void fillAlmostFull(char field[3][3], bool *pt); #endif \ No newline at end of file From 5c7aa87a03e1763b8c28abe414063ce8dfa0e8e8 Mon Sep 17 00:00:00 2001 From: Aimee Reincke Date: Thu, 2 Feb 2023 17:50:17 +0100 Subject: [PATCH 17/28] added fillAlmostFull to prevent playerwin --- src/c/ticTacToe.c | 259 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 259 insertions(+) diff --git a/src/c/ticTacToe.c b/src/c/ticTacToe.c index f811deb..f51d022 100644 --- a/src/c/ticTacToe.c +++ b/src/c/ticTacToe.c @@ -412,4 +412,263 @@ void fillAlmostFull(char field[3][3], bool* pt) { return; } } + + if (field[0][0] == 'O') { + if (field[0][1] == 'O') { + field[0][2] = 'X'; + *pt = true; + return; + } + else if (field[0][2] == 'O') { + field[0][1] = 'X'; + *pt = true; + return; + } + else if (field[1][0] == 'O') { + field[2][0] = 'X'; + *pt = true; + return; + } + else if (field[2][0] == 'O') { + field[1][0] = 'X'; + *pt = true; + return; + } + else if (field[1][1] == 'O') { + field[2][2] = 'X'; + *pt = true; + return; + } + else if (field[2][2] == 'O') { + field[1][1] = 'X'; + *pt = true; + return; + } + } + if (field[0][1] == 'O') { + if (field[0][0] == 'O') { + field[0][2] = 'X'; + *pt = true; + return; + } + else if (field[0][2] == 'O') { + field[0][0] = 'X'; + *pt = true; + return; + } + else if (field[1][1] == 'O') { + field[2][1] = 'X'; + *pt = true; + return; + } + else if (field[2][1] == 'O') { + field[1][1] = 'X'; + *pt = true; + return; + } + } + if (field[0][2] == 'O') { + if (field[0][0] == 'O') { + field[0][1] = 'X'; + *pt = true; + return; + } + else if (field[0][1] == 'O') { + field[0][0] = 'X'; + *pt = true; + return; + } + else if (field[1][1] == 'O') { + field[2][0] = 'X'; + *pt = true; + return; + } + else if (field[2][0] == 'O') { + field[1][1] = 'X'; + *pt = true; + return; + } + else if (field[1][2] == 'O') { + field[2][2] = 'X'; + *pt = true; + return; + } + else if (field[2][2] == 'O') { + field[1][2] = 'X'; + *pt = true; + return; + } + } + if (field[1][0] == 'O') { + if (field[1][1] == 'O') { + field[1][2] = 'X'; + *pt = true; + return; + } + else if (field[1][2] == 'O') { + field[1][1] = 'X'; + *pt = true; + return; + } + else if (field[0][0] == 'O') { + field[2][0] = 'X'; + *pt = true; + return; + } + else if (field[2][0] == 'O') { + field[0][0] = 'X'; + *pt = true; + return; + } + } + if (field[1][1] == 'O') { + if (field[1][0] == 'O') { + field[1][2] = 'X'; + *pt = true; + return; + } + else if (field[1][2] == 'O') { + field[1][0] = 'X'; + *pt = true; + return; + } + else if (field[0][1] == 'O') { + field[2][1] = 'X'; + *pt = true; + return; + } + else if (field[2][1] == 'O') { + field[0][1] = 'X'; + *pt = true; + return; + } + else if (field[0][0] == 'O') { + field[2][2] = 'X'; + *pt = true; + return; + } + else if (field[2][2] == 'O') { + field[0][0] = 'X'; + *pt = true; + return; + } + else if (field[0][2] == 'O') { + field[2][0] = 'X'; + *pt = true; + return; + } + else if (field[2][0] == 'O') { + field[0][2] = 'X'; + *pt = true; + return; + } + } + if (field[1][2] == 'O') { + if (field[1][0] == 'O') { + field[1][1] = 'X'; + *pt = true; + return; + } + else if (field[1][1] == 'O') { + field[1][0] = 'X'; + *pt = true; + return; + } + else if (field[0][2] == 'O') { + field[2][2] = 'X'; + *pt = true; + return; + } + else if (field[2][2] == 'O') { + field[0][2] = 'X'; + *pt = true; + return; + } + } + if (field[2][0] == 'O') { + if (field[2][1] == 'O') { + field[2][2] = 'X'; + *pt = true; + return; + } + else if (field[2][2] == 'O') { + field[2][1] = 'X'; + *pt = true; + return; + } + else if (field[0][0] == 'O') { + field[1][0] = 'X'; + *pt = true; + return; + } + else if (field[1][0] == 'O') { + field[0][0] = 'X'; + *pt = true; + return; + } + else if (field[1][1] == 'O') { + field[0][2] = 'X'; + *pt = true; + return; + } + else if (field[0][2] == 'O') { + field[1][1] = 'X'; + *pt = true; + return; + } + } + if (field[2][1] == 'O') { + if (field[2][0] == 'O') { + field[2][2] = 'X'; + *pt = true; + return; + } + else if (field[2][2] == 'O') { + field[2][0] = 'X'; + *pt = true; + return; + } + else if (field[1][1] == 'O') { + field[0][1] = 'X'; + *pt = true; + return; + } + else if (field[0][1] == 'O') { + field[1][1] = 'X'; + *pt = true; + return; + } + } + if (field[2][2] == 'O') { + if (field[2][0] == 'O') { + field[2][1] = 'X'; + *pt = true; + return; + } + else if (field[2][1] == 'O') { + field[2][0] = 'X'; + *pt = true; + return; + } + else if (field[0][2] == 'O') { + field[1][2] = 'X'; + *pt = true; + return; + } + else if (field[1][2] == 'O') { + field[0][2] = 'X'; + *pt = true; + return; + } + else if (field[1][1] == 'O') { + field[0][0] = 'X'; + *pt = true; + return; + } + else if (field[0][0] == 'O') { + field[1][1] = 'X'; + *pt = true; + return; + } + } } From 6e79888d3b42cef2164d7bd08f4b55da36a224bd Mon Sep 17 00:00:00 2001 From: Aimee Reincke Date: Thu, 2 Feb 2023 17:59:45 +0100 Subject: [PATCH 18/28] added validation 1/9 --- src/c/ticTacToe.c | 72 +++++++++++++++++++++++++++++++---------------- 1 file changed, 48 insertions(+), 24 deletions(-) diff --git a/src/c/ticTacToe.c b/src/c/ticTacToe.c index f51d022..375fb8a 100644 --- a/src/c/ticTacToe.c +++ b/src/c/ticTacToe.c @@ -156,34 +156,46 @@ void getComputerInput(char field[3][3]){ void fillAlmostFull(char field[3][3], bool* pt) { if (field[0][0] == 'X') { if (field[0][1] == 'X') { - field[0][2] = 'X'; - *pt = true; - return; + if( field[0][2] = '-'){ + field[0][2] = 'X'; + *pt = true; + return; + } } else if (field[0][2] == 'X') { - field[0][1] = 'X'; - *pt = true; - return; + if( field[0][1] = '-'){ + field[0][1] = 'X'; + *pt = true; + return; + } } else if (field[1][0] == 'X') { - field[2][0] = 'X'; - *pt = true; - return; + if( field[2][0] = '-'){ + field[2][0] = 'X'; + *pt = true; + return; + } } else if (field[2][0] == 'X') { - field[1][0] = 'X'; - *pt = true; - return; + if( field[1][0] = '-'){ + field[1][0] = 'X'; + *pt = true; + return; + } } else if (field[1][1] == 'X') { + if( field[2][2] = '-'){ field[2][2] = 'X'; *pt = true; return; + } } else if (field[2][2] == 'X') { + if( field[1][1] = '-'){ field[1][1] = 'X'; *pt = true; return; + } } } if (field[0][1] == 'X') { @@ -415,34 +427,46 @@ void fillAlmostFull(char field[3][3], bool* pt) { if (field[0][0] == 'O') { if (field[0][1] == 'O') { - field[0][2] = 'X'; - *pt = true; - return; + if( field[0][2] = '-'){ + field[0][2] = 'X'; + *pt = true; + return; + } } else if (field[0][2] == 'O') { - field[0][1] = 'X'; - *pt = true; - return; + if( field[0][1] = '-'){ + field[0][1] = 'X'; + *pt = true; + return; + } } else if (field[1][0] == 'O') { - field[2][0] = 'X'; - *pt = true; - return; + if( field[2][0] = '-'){ + field[2][0] = 'X'; + *pt = true; + return; + } } else if (field[2][0] == 'O') { - field[1][0] = 'X'; - *pt = true; - return; + if( field[1][0] = '-'){ + field[1][0] = 'X'; + *pt = true; + return; + } } else if (field[1][1] == 'O') { + if( field[2][2] = '-'){ field[2][2] = 'X'; *pt = true; return; + } } else if (field[2][2] == 'O') { + if( field[1][1] = '-'){ field[1][1] = 'X'; *pt = true; return; + } } } if (field[0][1] == 'O') { From 93a43ef1a740a38e9be7df65a25b47fab340eeac Mon Sep 17 00:00:00 2001 From: Aimee Reincke Date: Thu, 2 Feb 2023 18:03:58 +0100 Subject: [PATCH 19/28] added validation 2/9 --- src/c/ticTacToe.c | 28 ++++++++++++++++++++++------ 1 file changed, 22 insertions(+), 6 deletions(-) diff --git a/src/c/ticTacToe.c b/src/c/ticTacToe.c index 375fb8a..4fe0ad1 100644 --- a/src/c/ticTacToe.c +++ b/src/c/ticTacToe.c @@ -200,24 +200,32 @@ void fillAlmostFull(char field[3][3], bool* pt) { } if (field[0][1] == 'X') { if (field[0][0] == 'X') { - field[0][2] = 'X'; - *pt = true; - return; + if( field[0][2] = '-'){ + field[0][2] = 'X'; + *pt = true; + return; + } } else if (field[0][2] == 'X') { + if( field[0][0] = '-'){ field[0][0] = 'X'; *pt = true; return; + } } else if (field[1][1] == 'X') { + if( field[2][1] = '-'){ field[2][1] = 'X'; *pt = true; return; + } } else if (field[2][1] == 'X') { + if( field[1][1] = '-'){ field[1][1] = 'X'; *pt = true; return; + } } } if (field[0][2] == 'X') { @@ -471,24 +479,32 @@ void fillAlmostFull(char field[3][3], bool* pt) { } if (field[0][1] == 'O') { if (field[0][0] == 'O') { - field[0][2] = 'X'; - *pt = true; - return; + if( field[0][2] = '-'){ + field[0][2] = 'X'; + *pt = true; + return; + } } else if (field[0][2] == 'O') { + if( field[0][0] = '-'){ field[0][0] = 'X'; *pt = true; return; + } } else if (field[1][1] == 'O') { + if( field[2][1] = '-'){ field[2][1] = 'X'; *pt = true; return; + } } else if (field[2][1] == 'O') { + if( field[1][1] = '-'){ field[1][1] = 'X'; *pt = true; return; + } } } if (field[0][2] == 'O') { From a61a03975eab94c907ba5a519b6dc302243cdff4 Mon Sep 17 00:00:00 2001 From: Aimee Reincke Date: Thu, 2 Feb 2023 18:07:18 +0100 Subject: [PATCH 20/28] added validation 3/9 --- src/c/ticTacToe.c | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/src/c/ticTacToe.c b/src/c/ticTacToe.c index 4fe0ad1..4884cc2 100644 --- a/src/c/ticTacToe.c +++ b/src/c/ticTacToe.c @@ -230,34 +230,46 @@ void fillAlmostFull(char field[3][3], bool* pt) { } if (field[0][2] == 'X') { if (field[0][0] == 'X') { + if( field[0][1] = '-'){ field[0][1] = 'X'; *pt = true; return; + } } else if (field[0][1] == 'X') { + if( field[0][0] = '-'){ field[0][0] = 'X'; *pt = true; return; + } } else if (field[1][1] == 'X') { + if( field[2][0] = '-'){ field[2][0] = 'X'; *pt = true; return; + } } else if (field[2][0] == 'X') { + if( field[1][1] = '-'){ field[1][1] = 'X'; *pt = true; return; + } } else if (field[1][2] == 'X') { + if( field[2][2] = '-'){ field[2][2] = 'X'; *pt = true; return; + } } else if (field[2][2] == 'X') { + if( field[1][2] = '-'){ field[1][2] = 'X'; *pt = true; return; + } } } if (field[1][0] == 'X') { @@ -509,34 +521,46 @@ void fillAlmostFull(char field[3][3], bool* pt) { } if (field[0][2] == 'O') { if (field[0][0] == 'O') { + if( field[0][1] = '-'){ field[0][1] = 'X'; *pt = true; return; + } } else if (field[0][1] == 'O') { + if( field[0][0] = '-'){ field[0][0] = 'X'; *pt = true; return; + } } else if (field[1][1] == 'O') { + if( field[2][0] = '-'){ field[2][0] = 'X'; *pt = true; return; + } } else if (field[2][0] == 'O') { + if( field[1][1] = '-'){ field[1][1] = 'X'; *pt = true; return; + } } else if (field[1][2] == 'O') { + if( field[2][2] = '-'){ field[2][2] = 'X'; *pt = true; return; + } } else if (field[2][2] == 'O') { + if( field[1][2] = '-'){ field[1][2] = 'X'; *pt = true; return; + } } } if (field[1][0] == 'O') { From dfa05c2fdca507b5ca2e7741fbe17d84229ad53b Mon Sep 17 00:00:00 2001 From: Aimee Reincke Date: Thu, 2 Feb 2023 18:10:13 +0100 Subject: [PATCH 21/28] added validation 4/9 --- src/c/ticTacToe.c | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/src/c/ticTacToe.c b/src/c/ticTacToe.c index 4884cc2..1d77f9b 100644 --- a/src/c/ticTacToe.c +++ b/src/c/ticTacToe.c @@ -274,24 +274,32 @@ void fillAlmostFull(char field[3][3], bool* pt) { } if (field[1][0] == 'X') { if (field[1][1] == 'X') { + if( field[1][2] = '-'){ field[1][2] = 'X'; *pt = true; return; + } } else if (field[1][2] == 'X') { + if( field[1][1] = '-'){ field[1][1] = 'X'; *pt = true; return; + } } else if (field[0][0] == 'X') { + if( field[2][0] = '-'){ field[2][0] = 'X'; *pt = true; return; + } } else if (field[2][0] == 'X') { + if( field[0][0] = '-'){ field[0][0] = 'X'; *pt = true; return; + } } } if (field[1][1] == 'X') { @@ -565,24 +573,32 @@ void fillAlmostFull(char field[3][3], bool* pt) { } if (field[1][0] == 'O') { if (field[1][1] == 'O') { + if( field[1][2] = '-'){ field[1][2] = 'X'; *pt = true; return; + } } else if (field[1][2] == 'O') { + if( field[1][1] = '-'){ field[1][1] = 'X'; *pt = true; return; + } } else if (field[0][0] == 'O') { + if( field[2][0] = '-'){ field[2][0] = 'X'; *pt = true; return; + } } else if (field[2][0] == 'O') { + if( field[0][0] = '-'){ field[0][0] = 'X'; *pt = true; return; + } } } if (field[1][1] == 'O') { From 2b8f929ebebf5dfd151d0216ed8013a719f293c0 Mon Sep 17 00:00:00 2001 From: Aimee Reincke Date: Thu, 2 Feb 2023 18:16:30 +0100 Subject: [PATCH 22/28] added validation 5/9 --- src/c/ticTacToe.c | 112 +++++++++++++++++++++++++++++----------------- 1 file changed, 72 insertions(+), 40 deletions(-) diff --git a/src/c/ticTacToe.c b/src/c/ticTacToe.c index 1d77f9b..54bbc01 100644 --- a/src/c/ticTacToe.c +++ b/src/c/ticTacToe.c @@ -156,42 +156,42 @@ void getComputerInput(char field[3][3]){ void fillAlmostFull(char field[3][3], bool* pt) { if (field[0][0] == 'X') { if (field[0][1] == 'X') { - if( field[0][2] = '-'){ + if( field[0][2] == '-'){ field[0][2] = 'X'; *pt = true; return; } } else if (field[0][2] == 'X') { - if( field[0][1] = '-'){ + if( field[0][1] == '-'){ field[0][1] = 'X'; *pt = true; return; } } else if (field[1][0] == 'X') { - if( field[2][0] = '-'){ + if( field[2][0] == '-'){ field[2][0] = 'X'; *pt = true; return; } } else if (field[2][0] == 'X') { - if( field[1][0] = '-'){ + if( field[1][0] == '-'){ field[1][0] = 'X'; *pt = true; return; } } else if (field[1][1] == 'X') { - if( field[2][2] = '-'){ + if( field[2][2] == '-'){ field[2][2] = 'X'; *pt = true; return; } } else if (field[2][2] == 'X') { - if( field[1][1] = '-'){ + if( field[1][1] == '-'){ field[1][1] = 'X'; *pt = true; return; @@ -200,28 +200,28 @@ void fillAlmostFull(char field[3][3], bool* pt) { } if (field[0][1] == 'X') { if (field[0][0] == 'X') { - if( field[0][2] = '-'){ + if( field[0][2] == '-'){ field[0][2] = 'X'; *pt = true; return; } } else if (field[0][2] == 'X') { - if( field[0][0] = '-'){ + if( field[0][0] == '-'){ field[0][0] = 'X'; *pt = true; return; } } else if (field[1][1] == 'X') { - if( field[2][1] = '-'){ + if( field[2][1] == '-'){ field[2][1] = 'X'; *pt = true; return; } } else if (field[2][1] == 'X') { - if( field[1][1] = '-'){ + if( field[1][1] == '-'){ field[1][1] = 'X'; *pt = true; return; @@ -230,42 +230,42 @@ void fillAlmostFull(char field[3][3], bool* pt) { } if (field[0][2] == 'X') { if (field[0][0] == 'X') { - if( field[0][1] = '-'){ + if( field[0][1] == '-'){ field[0][1] = 'X'; *pt = true; return; } } else if (field[0][1] == 'X') { - if( field[0][0] = '-'){ + if( field[0][0] == '-'){ field[0][0] = 'X'; *pt = true; return; } } else if (field[1][1] == 'X') { - if( field[2][0] = '-'){ + if( field[2][0] == '-'){ field[2][0] = 'X'; *pt = true; return; } } else if (field[2][0] == 'X') { - if( field[1][1] = '-'){ + if( field[1][1] == '-'){ field[1][1] = 'X'; *pt = true; return; } } else if (field[1][2] == 'X') { - if( field[2][2] = '-'){ + if( field[2][2] == '-'){ field[2][2] = 'X'; *pt = true; return; } } else if (field[2][2] == 'X') { - if( field[1][2] = '-'){ + if( field[1][2] == '-'){ field[1][2] = 'X'; *pt = true; return; @@ -274,28 +274,28 @@ void fillAlmostFull(char field[3][3], bool* pt) { } if (field[1][0] == 'X') { if (field[1][1] == 'X') { - if( field[1][2] = '-'){ + if( field[1][2] == '-'){ field[1][2] = 'X'; *pt = true; return; } } else if (field[1][2] == 'X') { - if( field[1][1] = '-'){ + if( field[1][1] == '-'){ field[1][1] = 'X'; *pt = true; return; } } else if (field[0][0] == 'X') { - if( field[2][0] = '-'){ + if( field[2][0] == '-'){ field[2][0] = 'X'; *pt = true; return; } } else if (field[2][0] == 'X') { - if( field[0][0] = '-'){ + if( field[0][0] == '-'){ field[0][0] = 'X'; *pt = true; return; @@ -304,44 +304,60 @@ void fillAlmostFull(char field[3][3], bool* pt) { } if (field[1][1] == 'X') { if (field[1][0] == 'X') { + if( field[1][2] == '-'){ field[1][2] = 'X'; *pt = true; return; + } } else if (field[1][2] == 'X') { + if( field[1][0] == '-'){ field[1][0] = 'X'; *pt = true; return; + } } else if (field[0][1] == 'X') { + if( field[2][1] == '-'){ field[2][1] = 'X'; *pt = true; return; + } } else if (field[2][1] == 'X') { + if( field[0][1] == '-'){ field[0][1] = 'X'; *pt = true; return; + } } else if (field[0][0] == 'X') { + if( field[2][2] == '-'){ field[2][2] = 'X'; *pt = true; return; + } } else if (field[2][2] == 'X') { + if( field[0][0] == '-'){ field[0][0] = 'X'; *pt = true; return; + } } else if (field[0][2] == 'X') { + if( field[2][0] == '-'){ field[2][0] = 'X'; *pt = true; return; + } } else if (field[2][0] == 'X') { + if( field[0][2] == '-'){ field[0][2] = 'X'; *pt = true; return; + } } } if (field[1][2] == 'X') { @@ -455,42 +471,42 @@ void fillAlmostFull(char field[3][3], bool* pt) { if (field[0][0] == 'O') { if (field[0][1] == 'O') { - if( field[0][2] = '-'){ + if( field[0][2] == '-'){ field[0][2] = 'X'; *pt = true; return; } } else if (field[0][2] == 'O') { - if( field[0][1] = '-'){ + if( field[0][1] == '-'){ field[0][1] = 'X'; *pt = true; return; } } else if (field[1][0] == 'O') { - if( field[2][0] = '-'){ + if( field[2][0] == '-'){ field[2][0] = 'X'; *pt = true; return; } } else if (field[2][0] == 'O') { - if( field[1][0] = '-'){ + if( field[1][0] == '-'){ field[1][0] = 'X'; *pt = true; return; } } else if (field[1][1] == 'O') { - if( field[2][2] = '-'){ + if( field[2][2] == '-'){ field[2][2] = 'X'; *pt = true; return; } } else if (field[2][2] == 'O') { - if( field[1][1] = '-'){ + if( field[1][1] == '-'){ field[1][1] = 'X'; *pt = true; return; @@ -499,28 +515,28 @@ void fillAlmostFull(char field[3][3], bool* pt) { } if (field[0][1] == 'O') { if (field[0][0] == 'O') { - if( field[0][2] = '-'){ + if( field[0][2] == '-'){ field[0][2] = 'X'; *pt = true; return; } } else if (field[0][2] == 'O') { - if( field[0][0] = '-'){ + if( field[0][0] == '-'){ field[0][0] = 'X'; *pt = true; return; } } else if (field[1][1] == 'O') { - if( field[2][1] = '-'){ + if( field[2][1] == '-'){ field[2][1] = 'X'; *pt = true; return; } } else if (field[2][1] == 'O') { - if( field[1][1] = '-'){ + if( field[1][1] == '-'){ field[1][1] = 'X'; *pt = true; return; @@ -529,42 +545,42 @@ void fillAlmostFull(char field[3][3], bool* pt) { } if (field[0][2] == 'O') { if (field[0][0] == 'O') { - if( field[0][1] = '-'){ + if( field[0][1] == '-'){ field[0][1] = 'X'; *pt = true; return; } } else if (field[0][1] == 'O') { - if( field[0][0] = '-'){ + if( field[0][0] == '-'){ field[0][0] = 'X'; *pt = true; return; } } else if (field[1][1] == 'O') { - if( field[2][0] = '-'){ + if( field[2][0] == '-'){ field[2][0] = 'X'; *pt = true; return; } } else if (field[2][0] == 'O') { - if( field[1][1] = '-'){ + if( field[1][1] == '-'){ field[1][1] = 'X'; *pt = true; return; } } else if (field[1][2] == 'O') { - if( field[2][2] = '-'){ + if( field[2][2] == '-'){ field[2][2] = 'X'; *pt = true; return; } } else if (field[2][2] == 'O') { - if( field[1][2] = '-'){ + if( field[1][2] == '-'){ field[1][2] = 'X'; *pt = true; return; @@ -573,28 +589,28 @@ void fillAlmostFull(char field[3][3], bool* pt) { } if (field[1][0] == 'O') { if (field[1][1] == 'O') { - if( field[1][2] = '-'){ + if( field[1][2] == '-'){ field[1][2] = 'X'; *pt = true; return; } } else if (field[1][2] == 'O') { - if( field[1][1] = '-'){ + if( field[1][1] == '-'){ field[1][1] = 'X'; *pt = true; return; } } else if (field[0][0] == 'O') { - if( field[2][0] = '-'){ + if( field[2][0] == '-'){ field[2][0] = 'X'; *pt = true; return; } } else if (field[2][0] == 'O') { - if( field[0][0] = '-'){ + if( field[0][0] == '-'){ field[0][0] = 'X'; *pt = true; return; @@ -603,44 +619,60 @@ void fillAlmostFull(char field[3][3], bool* pt) { } if (field[1][1] == 'O') { if (field[1][0] == 'O') { + if( field[1][2] == '-'){ field[1][2] = 'X'; *pt = true; return; + } } else if (field[1][2] == 'O') { + if( field[1][0] == '-'){ field[1][0] = 'X'; *pt = true; return; + } } else if (field[0][1] == 'O') { + if( field[2][1] == '-'){ field[2][1] = 'X'; *pt = true; return; + } } else if (field[2][1] == 'O') { + if( field[0][1] == '-'){ field[0][1] = 'X'; *pt = true; return; + } } else if (field[0][0] == 'O') { + if( field[2][2] == '-'){ field[2][2] = 'X'; *pt = true; return; + } } else if (field[2][2] == 'O') { + if( field[0][0] == '-'){ field[0][0] = 'X'; *pt = true; return; + } } else if (field[0][2] == 'O') { + if( field[2][0] == '-'){ field[2][0] = 'X'; *pt = true; return; + } } else if (field[2][0] == 'O') { + if( field[0][2] == '-'){ field[0][2] = 'X'; *pt = true; return; + } } } if (field[1][2] == 'O') { From 02f5d1256a5b86e308855c58c87bcecf3cb759ec Mon Sep 17 00:00:00 2001 From: Aimee Reincke Date: Thu, 2 Feb 2023 18:18:53 +0100 Subject: [PATCH 23/28] added validation 6/9 --- src/c/ticTacToe.c | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/src/c/ticTacToe.c b/src/c/ticTacToe.c index 54bbc01..553ef9a 100644 --- a/src/c/ticTacToe.c +++ b/src/c/ticTacToe.c @@ -362,24 +362,32 @@ void fillAlmostFull(char field[3][3], bool* pt) { } if (field[1][2] == 'X') { if (field[1][0] == 'X') { + if( field[1][1] == '-'){ field[1][1] = 'X'; *pt = true; return; + } } else if (field[1][1] == 'X') { + if( field[1][0] == '-'){ field[1][0] = 'X'; *pt = true; return; + } } else if (field[0][2] == 'X') { + if( field[2][2] == '-'){ field[2][2] = 'X'; *pt = true; return; + } } else if (field[2][2] == 'X') { + if( field[0][2] == '-'){ field[0][2] = 'X'; *pt = true; return; + } } } if (field[2][0] == 'X') { @@ -677,24 +685,32 @@ void fillAlmostFull(char field[3][3], bool* pt) { } if (field[1][2] == 'O') { if (field[1][0] == 'O') { + if( field[1][1] == '-'){ field[1][1] = 'X'; *pt = true; return; + } } else if (field[1][1] == 'O') { + if( field[1][0] == '-'){ field[1][0] = 'X'; *pt = true; return; + } } else if (field[0][2] == 'O') { + if( field[2][2] == '-'){ field[2][2] = 'X'; *pt = true; return; + } } else if (field[2][2] == 'O') { + if( field[0][2] == '-'){ field[0][2] = 'X'; *pt = true; return; + } } } if (field[2][0] == 'O') { From cce629ba44bdf563845e2aa60607703e04d09c7d Mon Sep 17 00:00:00 2001 From: Aimee Reincke Date: Thu, 2 Feb 2023 18:22:12 +0100 Subject: [PATCH 24/28] added validation 7/9 --- src/c/ticTacToe.c | 28 ++++++++++++++++++++++++++-- 1 file changed, 26 insertions(+), 2 deletions(-) diff --git a/src/c/ticTacToe.c b/src/c/ticTacToe.c index 553ef9a..c7b19e5 100644 --- a/src/c/ticTacToe.c +++ b/src/c/ticTacToe.c @@ -392,34 +392,46 @@ void fillAlmostFull(char field[3][3], bool* pt) { } if (field[2][0] == 'X') { if (field[2][1] == 'X') { + if( field[2][2] == '-'){ field[2][2] = 'X'; *pt = true; return; + } } else if (field[2][2] == 'X') { + if( field[2][1] == '-'){ field[2][1] = 'X'; *pt = true; return; + } } else if (field[0][0] == 'X') { - field[1][0] = 'X'; + if( field[2][1] == '-'){ + field[2][1] = 'X'; *pt = true; return; + } } else if (field[1][0] == 'X') { + if( field[0][0] == '-'){ field[0][0] = 'X'; *pt = true; return; + } } else if (field[1][1] == 'X') { + if( field[0][2] == '-'){ field[0][2] = 'X'; *pt = true; return; + } } else if (field[0][2] == 'X') { + if( field[1][1] == '-'){ field[1][1] = 'X'; *pt = true; return; + } } } if (field[2][1] == 'X') { @@ -715,34 +727,46 @@ void fillAlmostFull(char field[3][3], bool* pt) { } if (field[2][0] == 'O') { if (field[2][1] == 'O') { + if( field[2][2] == '-'){ field[2][2] = 'X'; *pt = true; return; + } } else if (field[2][2] == 'O') { + if( field[2][1] == '-'){ field[2][1] = 'X'; *pt = true; return; + } } else if (field[0][0] == 'O') { - field[1][0] = 'X'; + if( field[2][1] == '-'){ + field[2][1] = 'X'; *pt = true; return; + } } else if (field[1][0] == 'O') { + if( field[0][0] == '-'){ field[0][0] = 'X'; *pt = true; return; + } } else if (field[1][1] == 'O') { + if( field[0][2] == '-'){ field[0][2] = 'X'; *pt = true; return; + } } else if (field[0][2] == 'O') { + if( field[1][1] == '-'){ field[1][1] = 'X'; *pt = true; return; + } } } if (field[2][1] == 'O') { From 4b3a0c31a51c3295968ac3817c34b749e6ebd06c Mon Sep 17 00:00:00 2001 From: Aimee Reincke Date: Thu, 2 Feb 2023 18:24:42 +0100 Subject: [PATCH 25/28] added validation 8/9 --- src/c/ticTacToe.c | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/src/c/ticTacToe.c b/src/c/ticTacToe.c index c7b19e5..89f5d2a 100644 --- a/src/c/ticTacToe.c +++ b/src/c/ticTacToe.c @@ -436,24 +436,32 @@ void fillAlmostFull(char field[3][3], bool* pt) { } if (field[2][1] == 'X') { if (field[2][0] == 'X') { + if( field[2][2] == '-'){ field[2][2] = 'X'; *pt = true; return; + } } else if (field[2][2] == 'X') { + if( field[2][0] == '-'){ field[2][0] = 'X'; *pt = true; return; + } } else if (field[1][1] == 'X') { + if( field[0][1] == '-'){ field[0][1] = 'X'; *pt = true; return; + } } else if (field[0][1] == 'X') { + if( field[1][1] == '-'){ field[1][1] = 'X'; *pt = true; return; + } } } if (field[2][2] == 'X') { @@ -771,24 +779,32 @@ void fillAlmostFull(char field[3][3], bool* pt) { } if (field[2][1] == 'O') { if (field[2][0] == 'O') { + if( field[2][2] == '-'){ field[2][2] = 'X'; *pt = true; return; + } } else if (field[2][2] == 'O') { + if( field[2][0] == '-'){ field[2][0] = 'X'; *pt = true; return; + } } else if (field[1][1] == 'O') { + if( field[0][1] == '-'){ field[0][1] = 'X'; *pt = true; return; + } } else if (field[0][1] == 'O') { + if( field[1][1] == '-'){ field[1][1] = 'X'; *pt = true; return; + } } } if (field[2][2] == 'O') { From f2c137dc9d8c29b6a6584496d7807a866255c536 Mon Sep 17 00:00:00 2001 From: Aimee Reincke Date: Thu, 2 Feb 2023 18:27:41 +0100 Subject: [PATCH 26/28] added validation 9/9 --- src/c/ticTacToe.c | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/src/c/ticTacToe.c b/src/c/ticTacToe.c index 89f5d2a..402c591 100644 --- a/src/c/ticTacToe.c +++ b/src/c/ticTacToe.c @@ -466,34 +466,46 @@ void fillAlmostFull(char field[3][3], bool* pt) { } if (field[2][2] == 'X') { if (field[2][0] == 'X') { + if( field[2][1] == '-'){ field[2][1] = 'X'; *pt = true; return; + } } else if (field[2][1] == 'X') { + if( field[2][0] == '-'){ field[2][0] = 'X'; *pt = true; return; + } } else if (field[0][2] == 'X') { + if( field[1][2] == '-'){ field[1][2] = 'X'; *pt = true; return; + } } else if (field[1][2] == 'X') { + if( field[0][2] == '-'){ field[0][2] = 'X'; *pt = true; return; + } } else if (field[1][1] == 'X') { + if( field[0][0] == '-'){ field[0][0] = 'X'; *pt = true; return; + } } else if (field[0][0] == 'X') { + if( field[1][1] == '-'){ field[1][1] = 'X'; *pt = true; return; + } } } @@ -809,34 +821,46 @@ void fillAlmostFull(char field[3][3], bool* pt) { } if (field[2][2] == 'O') { if (field[2][0] == 'O') { + if( field[2][1] == '-'){ field[2][1] = 'X'; *pt = true; return; + } } else if (field[2][1] == 'O') { + if( field[2][0] == '-'){ field[2][0] = 'X'; *pt = true; return; + } } else if (field[0][2] == 'O') { + if( field[1][2] == '-'){ field[1][2] = 'X'; *pt = true; return; + } } else if (field[1][2] == 'O') { + if( field[0][2] == '-'){ field[0][2] = 'X'; *pt = true; return; + } } else if (field[1][1] == 'O') { + if( field[0][0] == '-'){ field[0][0] = 'X'; *pt = true; return; + } } else if (field[0][0] == 'O') { + if( field[1][1] == '-'){ field[1][1] = 'X'; *pt = true; return; + } } } } From 47f60a02531ff98690e658c7d95336c703d18c5a Mon Sep 17 00:00:00 2001 From: Aimee Reincke Date: Thu, 2 Feb 2023 18:33:43 +0100 Subject: [PATCH 27/28] added play function --- src/c/ticTacToe.c | 25 +++++++++++++++++++++++++ src/c/ticTacToe.h | 1 + 2 files changed, 26 insertions(+) diff --git a/src/c/ticTacToe.c b/src/c/ticTacToe.c index 402c591..915e796 100644 --- a/src/c/ticTacToe.c +++ b/src/c/ticTacToe.c @@ -20,6 +20,7 @@ void printField(char field[3][3]){ } printf("\n"); } + printf("/n"); } void initField(char field[3][3]) { @@ -864,3 +865,27 @@ void fillAlmostFull(char field[3][3], bool* pt) { } } } + +int play() { + printPrompt(); + int counter = 0; + char field[3][3]; + initField(field); + int winner = NOWINNERYET; + while (winner == NOWINNERYET) { + if (counter % 2 == 0) { + printField(field); + getPlayerInput(field); + winner = wasGameWon(field); + } + else if (counter % 2 == 1) { + printField(field); + getComputerInput(field); + winner = wasGameWon(field); + } + counter += 1; + } + printField(field); + printf("%d", winner); + return winner; +} \ No newline at end of file diff --git a/src/c/ticTacToe.h b/src/c/ticTacToe.h index 6db536c..007b9cb 100644 --- a/src/c/ticTacToe.h +++ b/src/c/ticTacToe.h @@ -17,5 +17,6 @@ bool validatePlayerInput(int row, int col); int wasGameWon(char field[3][3]); void getComputerInput(char field[3][3]); void fillAlmostFull(char field[3][3], bool *pt); +int play(); #endif \ No newline at end of file From ed7690a49b00bc1406ace8ea492b6c71958caf81 Mon Sep 17 00:00:00 2001 From: Aimee Reincke Date: Thu, 2 Feb 2023 18:36:44 +0100 Subject: [PATCH 28/28] added print winner function --- src/c/ticTacToe.c | 14 +++++++++++++- src/c/ticTacToe.h | 1 + 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/src/c/ticTacToe.c b/src/c/ticTacToe.c index 915e796..610a8ec 100644 --- a/src/c/ticTacToe.c +++ b/src/c/ticTacToe.c @@ -886,6 +886,18 @@ int play() { counter += 1; } printField(field); - printf("%d", winner); + printWinner(winner); return winner; +} + +void printWinner(int winner){ + if(winner == PLAYER){ + printf("You won\n"); + } + else if(winner == COMPUTER){ + printf("I won\n"); + } + else if(winner == DRAW){ + printf("It's a draw\n"); + } } \ No newline at end of file diff --git a/src/c/ticTacToe.h b/src/c/ticTacToe.h index 007b9cb..7033e57 100644 --- a/src/c/ticTacToe.h +++ b/src/c/ticTacToe.h @@ -18,5 +18,6 @@ int wasGameWon(char field[3][3]); void getComputerInput(char field[3][3]); void fillAlmostFull(char field[3][3], bool *pt); int play(); +void printWinner(int winner); #endif \ No newline at end of file