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