Hi,
I have the most of the program working. Problem I'm having right now is that the game ends after 3 turns, rather than checking if player has gotten 3 of his/her "X" or "O" diagonally/horizontal/vertical. Function which checks if given player wins is called "bool checkwinner(char check)". Any help would be appreciated. Thank you for your time.
Code:
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
char board[] = " 1 | 2 | 3\n-----------\n 4 | 5 | 6\n-----------\n 7 | 8 | 9";
// Used to display the current state of the board.
int checkboard[] ={1,2,3,4,5,6,7,8,9,1,4,7,2,5,8,3,6,9,1,5,9,3,5,7};
// Used to figure out winning combinations: three blocks in a row
// vertically, horizontally or diagonally
// Challenge: Figure out the logic in this!
bool spaces[10]; // A record of which spaces (1..9) are free(true) or taken (false)
// Element 0 not used because the blocks are numbers 1..9
int nSpaces; // A count of the number of free spaces left.
bool moveHandler(int player,int n);
bool checkwinner(char check);
int playerinput(int player);
int k=0, j=0;
int main(void)
{
int player; // Identifier of current player
int key; // Key entered by user
int status;
bool terminationCondition; // Placeholder - to be replaced by you.
player = rand() %2 +1; // Randomly choose an initial player.
// Initial all spaces to be free
terminationCondition = false;
while (status != 1) // To be replaced by your logic for terminating a round
{
// Alternate between the two players
if (player == 1) {
player = 2;
} else {
player = 1;
}
// Print the current board
printf ("\n\n %s", board);
// Prompt the user for their next input
key = playerinput(player);
// Make the move keyed in by the user
status = moveHandler(player,key);
printf("%d\n",status);
// Decide if you have a winner or if the game is over.
}
system ("pause");
return 0;
}
/* Implements the next move in the tic-tac-toe game
* marking the square as taken
*
* @param player Identifier of the current player
* @param space The block selected (input) by the user
* @return Return true if the move is valid, false if the space is already taken
*/
bool moveHandler(int player,int space)
{
char check;
int i;
// set X for player one, or O for player 2
if (player == 2) {
check = 'O';
} else {
check = 'X';
}
// Check if block(or space) is already used. If so, re-prompt the user
// for another selection.
while (spaces[space] == true)
{
printf ("\nInvalid Space");
space=playerinput(player);
}
// Remove block from spaces array: Make the block(or space) as taken.
// Reduce the number of free-spaces by one.
spaces[space]=true;
//nSpaces--;
// Update the display of the board by replacing the block with check mark in board
// eg. Replace the number 9 with 'x'
// eg. Replace the number 6 with 'o'
// To convert a character into its integer equivalent use:
// char aChar;
// int intValue = aChar - '0';
// If aChar was character '9', then intValue will be the number 9.
char chSpace = (char)(((int)'0')+space); //I didn't understand the above method so I used my own method, it converts int to char
for (i==0;i<60;i++) {
if (chSpace == board[i]) {
board[i] = check;
}
}
// Replace space with check mark in checkboard array
if (player == 1) {
checkboard[space] = 11; // this means this place is taken by player 1
}
if (player == 2) {
checkboard[space] = 22; //this means this place is taken by player 2
}
// This will help us figure out if there is 3-in-a-row ... if we've won yet.
// Run the checkwinner function
return checkwinner(check);
}
/* Checkwinner function
*
* This function checks whether or not a player has a winning
* combination based on the checkboard array.
* @param check The character for the current player's turn (x or o)
* @return True if the player has 3-in-a-row; false otherwise.
*/
bool checkwinner(char check)
{
// Set the array element variables
int i;
for (i=1;i<24;i++) {
printf("%d ",checkboard[i]);
}
// Loop through checkboard to check winner
if (checkboard[0] == 11) {
k++;
}
if (checkboard[0] == 22) {
j++;
}
for (i=1;i<24;i++) {
if (i%3 == 0) {
k = 0;
j = 0;
}
if (checkboard[i] == 11) {
k++;
}
if (checkboard[i] == 22) {
j++;
}
if (k == 3) {
return true;
}
if (j == 3) {
return true;
}
else {
return false;
}
}
}
/* Prompts the player for their choice of block
* @param player Identifier of current player
* @return The number of the block that they want to put an 'x' or 'o' in. (1..9)
*/
int playerinput(int player)
{
int key;
do
{
printf ("\n\nPlayer %d : Please select a space ", player);
int status = scanf("%d",&key);
} while ( (key<1) && (key>9) );
return key;
}