I got a call from my friend asking me to help with his college assignment on C programming. It had a 3x3 Sudoku solver on list which I had worked earlier here using MATLAB; so I picked up a tic tac toe game, both multiplayer and Computer opponent.
I didn't use Google this time and came up with a fully home-made logic recipe which you can find below :
Here is the formal initialization parts and I declared a function called printer() which prints my tic tac toe game board along with 'x' and 'o' pieces every time called.
So this is an another function called win() which is a pretty long one; long but dumb. I just ran three to four loops to check horizontally, vertically and both diagonally for a three same 'x' or 'o's and return a flag if found one, implying the end of game. The final case is a no space on game board and all place filled with 'x' or 'o' declaring a draw. It also prints the winners name. (Don't worry I will add a link to the complete code if you want to look through it)
This is my main() function. An infinite loop runs till 'e' character is pressed or a win or draw flag is raised. In this infinite loop, an input is taken from the player whenever the chanceflag is 0 implying that it is Player's chance. 'w','a','s' and 'd' keys are used to print a "_" on the game board to show cursor position. When a ' '(space) is hit, the program checks for that place to be empty and if empty then the user is allowed to input a character. The character is permanently placed there if it is 'x'. The chanceflag is set 1 once input is confirmed.
Next comes the A.I. part.
The c_row and c_col variables contain position of last input from player. The Computer performs a check for two 'x' on the same c_row,c_col and cases where the place is on diagonal; c_row=c_col(main diagonal) or c_row=2-c_col(anti diagonal). If 2 'x' are found then the priority of program is to fill the empty space with an 'o'; otherwise it fills the space with a randomly picked value of row and column using rand()/15000(Because maximum value of rand() is 32767, i.e. if divided by 15000 quotient is between 0 and 2). Labels have served a great purpose in program, check them for sure.
You can find the complete code here : Tic Tac Toe using simple A.I.
Do comment a better approach to this program! Thank you!
I didn't use Google this time and came up with a fully home-made logic recipe which you can find below :
Here is the formal initialization parts and I declared a function called printer() which prints my tic tac toe game board along with 'x' and 'o' pieces every time called.
So this is an another function called win() which is a pretty long one; long but dumb. I just ran three to four loops to check horizontally, vertically and both diagonally for a three same 'x' or 'o's and return a flag if found one, implying the end of game. The final case is a no space on game board and all place filled with 'x' or 'o' declaring a draw. It also prints the winners name. (Don't worry I will add a link to the complete code if you want to look through it)
This is my main() function. An infinite loop runs till 'e' character is pressed or a win or draw flag is raised. In this infinite loop, an input is taken from the player whenever the chanceflag is 0 implying that it is Player's chance. 'w','a','s' and 'd' keys are used to print a "_" on the game board to show cursor position. When a ' '(space) is hit, the program checks for that place to be empty and if empty then the user is allowed to input a character. The character is permanently placed there if it is 'x'. The chanceflag is set 1 once input is confirmed.
Next comes the A.I. part.
The c_row and c_col variables contain position of last input from player. The Computer performs a check for two 'x' on the same c_row,c_col and cases where the place is on diagonal; c_row=c_col(main diagonal) or c_row=2-c_col(anti diagonal). If 2 'x' are found then the priority of program is to fill the empty space with an 'o'; otherwise it fills the space with a randomly picked value of row and column using rand()/15000(Because maximum value of rand() is 32767, i.e. if divided by 15000 quotient is between 0 and 2). Labels have served a great purpose in program, check them for sure.
You can find the complete code here : Tic Tac Toe using simple A.I.
Do comment a better approach to this program! Thank you!