aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--Tic Tac Toe.c135
1 files changed, 135 insertions, 0 deletions
diff --git a/Tic Tac Toe.c b/Tic Tac Toe.c
new file mode 100644
index 0000000..2506cd0
--- /dev/null
+++ b/Tic Tac Toe.c
@@ -0,0 +1,135 @@
1#include<stdio.h>
2#include<stdlib.h>
3#include<ctype.h>
4#include<string.h>
5char check_board(char[10],char[10]);
6int print_board(char[10]);
7int check_tied(char[10]);
8// 0|1|2
9// - - -
10// 3|4|5
11// - - -
12// 6|7|8
13
14int main(void){
15 char board[10] = " ";
16 char vis_board[10] = "123456789";
17 int x_turn = 1;
18 int o_turn = 0;
19 int block;
20 char gg[10];
21 char *ptr;
22
23 //main game loop
24 while (1){
25
26 while (1){
27 print_board(vis_board);
28 if (x_turn){
29 printf("x's turn");
30 } else {
31 printf("o's turn");
32 }
33 printf("\nWhich block? ");
34 //------------Input validation
35 scanf("%s",gg);
36 block = strtol(gg,&ptr,10);
37 //printf("-%u-\n",*ptr);
38 if (*ptr != 0){
39 printf("Please enter a interger(1-9)\n");
40 continue;
41 }
42 if (board[block-1] == 'x' || board[block-1] == 'o' || block == 0){
43 printf("%d already taken\n",block);
44 } else {
45 if (x_turn){
46 board[block-1] = 'x';
47 vis_board[block-1] = 'x';
48 } else if (o_turn){
49 board[block-1] = 'o';
50 vis_board[block-1] = 'o';
51 }
52 break;
53 }
54
55 }
56
57
58 //checks if win condition met or tie
59 if (check_board(vis_board,board) == 1){
60 if (x_turn){
61 print_board(vis_board);
62 printf("\ncongratz %c wins!\n",'x');
63 }
64 if (o_turn){
65 print_board(vis_board);
66 printf("\ncongratz %c wins!\n",'x');
67 }
68 break;
69 } else if (check_board(vis_board,board) == 2){
70 print_board(vis_board);
71 printf("\nGame tied\n");
72 break;
73 }
74
75 x_turn = !x_turn;
76 o_turn = !o_turn;
77
78
79 }
80
81 return(0);
82}
83
84int print_board(char board[10]){
85 for (int i = 0; i < strlen(board); ++i){
86 printf("%c",board[i]);
87 if (i != 2 && i != 5 && i != 8){
88 printf("|");
89 } else if (i != 8){
90 printf("\n- - -\n");
91 }
92 }
93 printf("\n");
94 return(0);
95}
96
97char check_board(char board[10],char board2[10]){
98 //return 1 when win condition 0 otherwise 2 if game tied
99 //horizontal
100 if (board[0] == board[1] && board[0] == board[2]){
101 return (1);
102 } else if (board[3] == board[4] && board[3] == board[5]){
103 return board[3];
104 } else if (board[6] == board[7] && board[6] == board[8]){
105 return (1);
106 //vertical
107 } else if (board[0] == board[3] && board[0] == board[6]){
108 return (1);
109 } else if (board[1] == board[4] && board[1] == board[7]){
110 return (1);
111 } else if (board[2] == board[5] && board[2] == board[8]){
112 return (1);
113 //diagonal
114 } else if (board[0] == board[4] && board[0] == board[8]){
115 return (1);
116 } else if (board[2] == board[4] && board[2] == board[6]){
117 return (1);
118 //check if tied game
119 } else if (check_tied(board2)){
120 return(2);
121 }else {
122 return(0);
123 }
124}
125
126
127int check_tied(char board[10]){
128 //run through board if ' ' is found return 0 else return 1
129 for (int i = 0; i < 9; ++i){
130 if (board[i] == ' '){
131 return(0);
132 }
133 }
134 return(1);
135}