-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNewMain.c
173 lines (146 loc) · 5.29 KB
/
NewMain.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <conio.h>
void markArray(); //Takes in a number from the users, makes sure it is valid, and creates a new array
void drawBoard(); // Draws the board with the given array for each turn
bool checkForWin(); // checks for a win each time a new board is drawn
char nums[9] = {'1','2','3','4','5','6','7','8','9'}; //square numbers
char mark; // is equal to X or O based on player number
int playerNum = 1; // initialize the player number
bool winFound = 1; // initialize the that the win is not found
int turnCounter = 0; // counts the number of moves that have been made
int main ()
{
printf("Welcome to my TicTacToe Game!\n");
printf("Player 1 is X \nPlayer 2 is O");
drawBoard();
while(winFound == 1 && turnCounter != 9)
{
markArray();
drawBoard();
checkForWin();
}
//Print out who won the game or if the game was a draw
if (turnCounter == 9)
{
printf("The Game is a Draw\n\n");
}
else
{
playerNum = (playerNum == 1) ? 2 : 1;
printf("Player %d Wins!\n\n", playerNum);
}
return 0;
}
/*************************************************************************
The function markArray;
1. Asks the user for a number.
2. Checks if the number is valid based on the current nums array.
*************************************************************************/
void markArray()
{
unsigned char squareNumChar; //this is the user input variable for square number
bool validNum = 1; // initialize the number to be invalid
int i = 0; //initialize counter variable
int squareNumInt; //used to check validity of input
mark = (playerNum == 1) ? 'X' : 'O'; //This if else statement sets mark based on playerNum
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//Ask the player to input a number as an integer
printf("\n\nPlayer %d, enter a number: ", playerNum);
scanf("%d", &squareNumInt);
//Make a character variable out of the integer variable
squareNumChar = squareNumInt + '0'; // create character variable of squareNumInt
// printf("\nChosen number is %d\n\n", squareNumInt); // print out character variable to check
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//This loop makes sure the user enters a valid number
i = 0;
while(validNum == 1)
{
//This statement works when all of nums has been looped through.
//It sets i back to 0 and asks for a different value of squareNum
if (i == 9)
{
printf("\nPlease enter a valid number: ");
scanf("%d", &squareNumInt);
squareNumChar = squareNumInt + '0';
i = 0;
}
//This if statement works if the number entered is not equal any of the square numbers and increases the counter
if(squareNumChar != nums[i])
{
validNum = 1;
i++;
}
//This statement sets validNum equal to 0 if squarenum is equal to any value of nums and resets the counter
if (squareNumChar == nums[i])
{
validNum = 0;
i = 0;
turnCounter++; //increase the turn counter each time a valid move is made
system("cls"); //clear the console
}
}
nums[squareNumInt-1] = mark; //changes the square chosen to the given mark
//Switch the player number each turn
playerNum = (playerNum == 1) ? 2 : 1;
}
/*******************************************************
The function drawBoard:
1. Prints out the new board using the nums array.
*******************************************************/
void drawBoard()
{
//Print out the new board
printf("\n\n\n | | \n");
printf(" %c | %c | %c \n", nums[0], nums[1], nums[2]);
printf("_____|_____|_____\n");
printf(" | | \n");
printf(" %c | %c | %c \n", nums[3], nums[4], nums[5]);
printf("_____|_____|_____\n");
printf(" | | \n");
printf(" %c | %c | %c \n", nums[6], nums[7], nums[8]);
printf(" | | \n\n");
}
/************************************************************
The Function checkForWin:
1. Checks the columns for a win
2. Checks the rows for a win
3. Checks the diagonal and anti-diagonal for a win
************************************************************/
bool checkForWin()
{
int i;
//Check for win in rows
for (i = 0; i < 9; i+= 3)
{
if(nums[i] == nums[i+1] && nums[i+1] == nums[i+2] && nums[i+2] == mark)
{
winFound = 0;
turnCounter = 0;
}
}
//Check for win in columns
for (i = 0; i < 3; i++)
{
if(nums[i] == nums[i+3] && nums[i+3] == nums[i+6] && nums[i+6] == mark)
{
winFound = 0;
turnCounter = 0;
}
}
//Check for win in diagonal
if(nums[0] == nums[4] && nums[4] == nums[8] && nums[8] == mark)
{
winFound = 0;
turnCounter = 0;
}
//Check for win in anti-diagonal
if(nums[2] == nums[4] && nums [4] == nums[6] && nums[6] == mark)
{
winFound = 0;
turnCounter = 0;
}
//Return whether or not someone won
return winFound;
}