Occupied Position in Tic-Tac-Toe Game [C] - c

One question about this code: if the choice the player picks is already taken, how do I make him/her choose again?
If I compile the code now, it will change the the position for the one the user chose, and this can't happen.
Obs: the "drawing" of the tic-tac-toe game should appear before this ("\nInform your choice (1-9): ")
This is the code:
char mat[9];
int main(void)
{
int i,jogada,casa,jogo=0;
for(i=0; i<= 9; i++)
mat[i] = ' ';
do{
printf("\n");
printf("\t|\t|\n");
printf("\t|\t|\n");
printf("%c\t|%c\t|%c\n",mat[1],mat[2],mat[3]);
printf("--------|-------|-------\n");
printf("\t|\t|\n");
printf("%c\t|%c\t|%c\n",mat[4],mat[5],mat[6]);
printf("--------|-------|-------\n");
printf("\t|\t|\n");
printf("\t|\t|\n");
printf("%c\t|%c\t|%c\n\n",mat[7],mat[8],mat[9]);
printf("\nJOGADOR X ");
printf("\nInforme uma posicao livre (1-9): ");
scanf("%d",&casa);
}while((casa>=1)&&(casa<=9));
if((casa>=1)&&(casa<=9))
{
if(mat[casa] == 'X' || mat[casa] == 'O')
printf(" ");
else
mat[casa] = 'X';
}
printf("\n");
printf("\t|\t|\n");
printf("\t|\t|\n");
printf("%c\t|%c\t|%c\n",mat[1],mat[2],mat[3]);
printf("--------|-------|-------\n");
printf("\t|\t|\n");
printf("%c\t|%c\t|%c\n",mat[4],mat[5],mat[6]);
printf("--------|-------|-------\n");
printf("\t|\t|\n");
printf("\t|\t|\n");
printf("%c\t|%c\t|%c\n\n",mat[7],mat[8],mat[9]);
if(mat[1] != ' ' && mat[1] == mat[2] && mat[2] == mat[3]) {
jogo++;
printf("X Ganhou!\n");
break; }
else if(mat[4] != ' ' && mat[4] == mat[5] && mat[5] == mat[6]) {
printf("X Ganhou!\n");
jogo++;
break; }
else if(mat[7] != ' ' && mat[7] == mat[8] && mat[8] == mat[9]) {
printf("X Ganhou!\n");
jogo++;
break; }
else if(mat[1] != ' ' && mat[1] == mat[4] && mat[4] == mat[7]) {
printf("X Ganhou!\n");
jogo++;
break;}
else if(mat[2] != ' ' && mat[2] == mat[5] && mat[5] == mat[8]) {
printf("X Ganhou!\n");
jogo++;
break; }
else if(mat[3] != ' ' && mat[3] == mat[6] && mat[6] == mat[9]) {
printf("X Ganhou!\n");
jogo++;
break; }
else if(mat[1] != ' ' && mat[1] == mat[5] && mat[5] == mat[9]){
printf("X Ganhou!\n");
jogo++;
break;}
else if(mat[7] != ' ' && mat[7] == mat[5] && mat[5] == mat[3]) {
printf("X Ganhou!\n");
jogo++;
break; }
else if((mat[1] != ' ') && (mat[2] != ' ') && (mat[3] != ' ') && (mat[4] != ' ') && (mat[5] != ' ') && (mat[6] != ' ') && (mat[7] != ' ') && (mat[8] != ' ') && (mat[9] = ' ')){
printf("Velha!\n");
jogo++;
break;
}
printf("\nJOGADOR O ");
printf("\nInforme uma posicao livre (1-9): ");
scanf("%d",&casa);
if((casa>=1)&&(casa<=9))
{
if(mat[casa] == 'X' || mat[casa] == 'O')
;
else
mat[casa] = 'O';
}
printf("\n");
printf("\t|\t|\n");
printf("\t|\t|\n");
printf("%c\t|%c\t|%c\n",mat[1],mat[2],mat[3]);
printf("--------|-------|-------\n");
printf("\t|\t|\n");
printf("%c\t|%c\t|%c\n",mat[4],mat[5],mat[6]);
printf("--------|-------|-------\n");
printf("\t|\t|\n");
printf("\t|\t|\n");
printf("%c\t|%c\t|%c\n\n",mat[7],mat[8],mat[9]);
if(mat[1] != ' ' && mat[1] == mat[2] && mat[2] == mat[3]) {
printf("O Ganhou!\n");
jogo++;
break; }
else if(mat[4] != ' ' && mat[4] == mat[5] && mat[5] == mat[6]) {
printf("O Ganhou!\n");
jogo++;
break; }
else if(mat[7] != ' ' && mat[7] == mat[8] && mat[8] == mat[9]) {
printf("O Ganhou!\n");
jogo++;
break; }
else if(mat[1] != ' ' && mat[1] == mat[4] && mat[4] == mat[7]) {
printf("O Ganhou!\n");
jogo++;
break; }
else if(mat[2] != ' ' && mat[2] == mat[5] && mat[5] == mat[8]) {
printf("O Ganhou!\n");
jogo++;
break;}
else if(mat[3] != ' ' && mat[3] == mat[6] && mat[6] == mat[9]) {
printf("O Ganhou!\n");
jogo++;
break; }
else if(mat[1] != ' ' && mat[1] == mat[5] && mat[5] == mat[9]){
printf("O Ganhou!\n");
jogo++;
break;}
else if(mat[7] != ' ' && mat[7] == mat[5] && mat[5] == mat[3]) {
printf("O Ganhou!\n");
jogo++;
break; }
else if((mat[1] != ' ') && (mat[2] != ' ') && (mat[3] != ' ') && (mat[4] != ' ') && (mat[5] != ' ') && (mat[6] != ' ') && (mat[7] != ' ') && (mat[8] != ' ') && (mat[9] = ' ')){
printf("Velha!\n");
jogo++;
break;
}
}
system("PAUSE");
return 0;
}

If the choice the player picks is already taken, how do I make him/her choose again?
Change your scanf() in 2 locations (X and O)
do {
printf("\nInforme uma posicao livre (1-9): ");
scanf("%d",&casa);
} while( (casa<1) || (casa>9) || (mat[casa] != ' ') );
You do have a bigger problem.
Change char mat[9] to char mat[10]. You are indexing your mat[] from 1 to 9 throughout your code. But char mat[9] only allows mat[0] to mat[8]. The quick fix is to make mat[] 1 char longer and not use mat[0]. Leave for(i=0; i<= 9; i++) as is.
OR -
A proper fix would be to keep char mat[9] and change all the many mat[i] references to mat[i-1]. e.g. mat[5] becomes mat[4]. Change to for(i=0; i < 9; i++).

Related

Tic Tac Toe using C

Brief Explanation of the Problem - The aim of the code is to make a basic tic tac toe game using C. There are two players X and O , both can enter various numbers as choice from 1-9 for each individual chance.
The game board is similar to a 3 x 3 matrix, where -
Row 1 is for 1 to 3.
Row 2 is for 4 to 6.
Row 3 is for 7 to 9.
Any number except 1-9 will throw an error and will prompt the user to re-enter the number. Unfortunately, I'm getting the same Invalid input error for a valid input. Everything else seems to work except my loop.
Here's the code for reference -
#include<stdio.h> //Tic Tac Toe
#include<stdlib.h>
#include<conio.h>
char square[10] = {'0','1','2','3','4','5','6','7','8','9'};
int choice, player;
int checkForWin();
void displayBoard();
void mrkBoard(char mark);
int main()
{
int i;
char mark;
player = 1;
do
{
displayBoard();
player = (player % 2) ? 1:2;
printf("Player %d, enter the number: ",player);
scanf("%d",&choice);
mark = (player == 1) ? 'X' : 'O';
mrkBoard(mark);
i = checkForWin();
player++;
}while(i == -1);
return 0;
}
int checkForWin()
{
int returnValue = 0;
if (square[1] == square[2] && square[2] == square[3])
{
returnValue = 1;
}
else if (square[4] == square[5] && square[5] == square[6])
returnValue = 1;
else if (square[7] == square[8] && square[8] == square[9])
returnValue = 1;
else if (square[1] == square[5] && square[5] == square[9])
returnValue = 1;
else if (square[3] == square[5] && square[5] == square[7])
returnValue = 1;
else if (square[1] == square[4] && square[4] == square[7])
returnValue = 1;
else if (square[2] == square[5] && square[5] == square[8])
returnValue = 1;
else if (square[3] == square[6] && square[6] == square[9])
returnValue = 1;
else if(square[1] != '1' && square[2] != '2' && square[3] != '3' && square[4] != '4' &&
square[5] != '5' && square[6] != '6' && square[7] != '7' &&
square[8] != '8' && square[9] != '9')
returnValue = 0;
else
returnValue = -1;
return returnValue;
}
void displayBoard()
{
system("cls");
printf("\n\nTic Tac Toe\n\n");
printf("Player 1 (X) - Player 2 (O)\n\n\n");
printf(" | | \n");
printf(" %c | %c | %c \n", square[1], square[2],square[3]);
printf("_____|_____|_____\n");
printf(" | | \n");
printf(" %c | %c | %c\n", square[4], square[5],square[6]);
printf("_____|_____|_____\n");
printf(" | | \n");
printf(" %c | %c | %c\n", square[7], square[8],square[9]);
printf(" | | \n\n");
}
void mrkBoard(char mark)
{
if (choice == 1 && square[1] == '1')
square[1] = mark;
else if (choice == 2 && square[1] == '2')
square[2] = mark;
else if (choice == 3 && square[1] == '3')
square[3] = mark;
else if (choice == 4 && square[1] == '4')
square[4] = mark;
else if (choice == 5 && square[1] == '5')
square[5] = mark;
else if (choice == 6 && square[1] == '6')
square[6] = mark;
else if (choice == 7 && square[1] == '7')
square[7] = mark;
else if (choice == 8 && square[1] == '8')
square[8] = mark;
else if (choice == 9 && square[1] == '9')
square[9] = mark;
else
{
printf("Invalid ");
player--;
getch();
}
}
#include<stdio.h> //Tic Tac Toe
#include<stdlib.h>
#include<conio.h>
char square[10] = {'0','1','2','3','4','5','6','7','8','9'};
int choice, player;
int checkForWin();
void displayBoard();
void mrkBoard(char mark);
int main()
{
int i;
char mark;
player = 1;
do
{
displayBoard();
player = (player % 2) ? 1:2;
printf("Player %d, enter the number: ",player);
scanf("%d",&choice);
mark = (player == 1) ? 'X' : 'O';
mrkBoard(mark);
i = checkForWin();
player++;
}while(i == -1);
return 0;
}
int checkForWin()
{
int returnValue = 0;
if (square[1] == square[2] && square[2] == square[3])
{
returnValue = 1;
}
else if (square[4] == square[5] && square[5] == square[6])
returnValue = 1;
else if (square[7] == square[8] && square[8] == square[9])
returnValue = 1;
else if (square[1] == square[5] && square[5] == square[9])
returnValue = 1;
else if (square[3] == square[5] && square[5] == square[7])
returnValue = 1;
else if (square[1] == square[4] && square[4] == square[7])
returnValue = 1;
else if (square[2] == square[5] && square[5] == square[8])
returnValue = 1;
else if (square[3] == square[6] && square[6] == square[9])
returnValue = 1;
else if(square[1] != '1' && square[2] != '2' && square[3] != '3' && square[4] != '4' &&
square[5] != '5' && square[6] != '6' && square[7] != '7' &&
square[8] != '8' && square[9] != '9')
returnValue = 0;
else
returnValue = -1;
return returnValue;
}
void displayBoard()
{
system("cls");
printf("\n\nTic Tac Toe\n\n");
printf("Player 1 (X) - Player 2 (O)\n\n\n");
printf(" | | \n");
printf(" %c | %c | %c \n", square[1], square[2],square[3]);
printf("_____|_____|_____\n");
printf(" | | \n");
printf(" %c | %c | %c\n", square[4], square[5],square[6]);
printf("_____|_____|_____\n");
printf(" | | \n");
printf(" %c | %c | %c\n", square[7], square[8],square[9]);
printf(" | | \n\n");
}
void mrkBoard(char mark)
{
if (choice == 1 && square[1] == '1')
square[1] = mark;
else if (choice == 2 && square[2] == '2')
square[2] = mark;
else if (choice == 3 && square[3] == '3')
square[3] = mark;
else if (choice == 4 && square[4] == '4')
square[4] = mark;
else if (choice == 5 && square[5] == '5')
square[5] = mark;
else if (choice == 6 && square[6] == '6')
square[6] = mark;
else if (choice == 7 && square[7] == '7')
square[7] = mark;
else if (choice == 8 && square[8] == '8')
square[8] = mark;
else if (choice == 9 && square[9] == '9')
square[9] = mark;
else
{
printf("Invalid ");
player--;
getch();
}
}

Implement a function to recursively return a valid win-condition - C

Issue: I'm trying to utilize recTest to check and return the winner of a simple Tic-Tac-Toe game.
I'm just unsure of what I need to pass in the call (line I've highlighted), and how to integrate my public square array.
I apologize if this is overly simple, I'm just having a rough time connecting the dots.
#include<stdio.h>
#include<conio.h>
char square[10] = {'o', '1', '2', '3', '4', '5', '6', '7', '8', '9'};
// returns -1 if no v[i] satisfies p
// returns i if v[i] satisfies p (picks largest i)
// (i>=0) and (i<n)
const int numwinpos = 8;
const int winpos[8][3] = {{1,2,3},
{4,5,6},
{7,8,9},
{1,4,7},
{2,5,8},
{3,6,9},
{1,5,9},
{3,5,7}};
//Function below, and variables above<--------------------------------------
int recTest(const int v[], const int n){
if( (n>0) && (!p(v[n-1])) )
return recTest(v,n-1);
else
return n-1;
}
/*int winnerCheck() {
//not even hard-coded tho...
if (square[1] == square[2] && square[2] == square[3])
return 1;
else if (square[4] == square[5] && square[5] == square[6])
return 1;
else if (square[7] == square[8] && square[8] == square[9])
return 1; //above 3 check across
else if (square[1] == square[4] && square[4] == square[7])
return 1;
else if (square[2] == square[5] && square[5] == square[8])
return 1;
else if (square[3] == square[6] && square[6] == square[9])
return 1; //above 3 check down
else if (square[1] == square[5] && square[5] == square[9])
return 1;
else if (square[3] == square[5] && square[5] == square[7])
return 1; //above 2 check diagonal
else if (square[1] != '1' && square[2] != '2' && square[3] != '3' && square[4]
!= '4' && square[5] != '5' &&
square[6] != '6' && square[7] != '7' && square[8]
!= '8' && square[9] != '9')
return 0;
else
//exit
return -1;
}
*/
void board() {
printf("\n\n\tTic Tac Toe\n\n");
printf("Player 1 (X) - Player 2 (O)\n\n\n");
//prints the board after every input
printf(" | | \n");
printf(" %c | %c | %c \n", square[1], square[2],square[3]);
printf("____|_____|____\n");
printf(" | | \n");
printf(" %c | %c | %c \n", square[4], square[5],square[6]);
printf("____|_____|____\n");
printf(" | | \n");
printf(" %c | %c | %c \n", square[7], square[8],square[9]);
printf(" | | \n");
}
int main() {
/*
char board[3][3] = {
};
printf("\t|\t|\t\n");
printf("\t|\t|\t\n");
printf("________|_______|________\n");
printf("\t|\t|\t\n");
printf("\t|\t|\t\n");
printf("________|_______|________\n");
printf("\t|\t|\t\n");
printf("\t|\t|\t\n");
printf("\t|\t|\t\n");
*/
int player = 1, i, choice;
char mark;
do {
board();
player = player % 2 ? 1 : 2;
printf("Player %d, enter a number: ", player);
scanf("%d", &choice);
//mark = (player == 1) ? 'X' : 'O';
if (player == 1) {
mark = 'X';
} else {
mark = 'O';
}
if (choice == 1)
square[1] = mark;
else if (choice == 2)
square[2] = mark;
else if (choice == 3)
square[3] = mark;
else if (choice == 4)
square[4] = mark;
else if (choice == 5)
square[5] = mark;
else if (choice == 6)
square[6] = mark;
else if (choice == 7)
square[7] = mark;
else if (choice == 8)
square[8] = mark;
else if (choice == 9)
square[9] = mark;
i = recTest(square, numwinpos); //HERE <--------------------------------------
player++;
} while (i == -1);
board();//call board
if (i == 1)
printf("----->\aPlayer %d WINS!<-----", --player);//nice alert sound when printed
else
printf("----->\aC-could it be...? Game draw!<-----");//nice alert sound when printed
//getch();//waits for user input before ending
return 0;
}
A quick and dirty way to use recursion would be to do something like below... and yes this can be optimized..
int winnerCheck(int x)
{
if(x < numwinpos)
{
if((square[winpos[x][0]] == 'x') && (square[winpos[x][1]] == 'x') && (square[winpos[x][2]] == 'x') ||
(square[winpos[x][0]] == 'o') && (square[winpos[x][1]] == 'o') && (square[winpos[x][2]] == 'o'))
return 1;
else
{
return winnerCheck(++x);
}
}
return 0;
}

Trying to poll for a MSP430's Keypad Input

I'm working on a really simplified version of space invaders. I generate some aliens then as time goes on I lower them closer to the bottom of a screen which I'm just using arrays for. I've got it generating and lowering the invaders but when I tried to hit the keypad button to delete an alien it was ignored. I believe its because I'm not constantly looking for the Keypad's input so my checkInput() function isn't doing what it should do. I wanted to poll for it but I am not sure how or where I should do it. Any help would be appreciated.
#include <msp430.h>
#include "peripherals.h"
#include <stdlib.h>
// Function Prototypes
void swDelay(char numLoops);
void countDown();
//void speedCheck();
void generateAliens();
void inputCheck();
void displayAliens();
void initLeds(void);
// Declare globals here
int game;
//int win;
//int turn;
//int levelSpeed;
char arrAliens0[5] = {' ', ' ', ' ', ' ', ' '};
char arrAliens1[5] = {' ', ' ', ' ', ' ', ' '};
char arrAliens2[5] = {' ', ' ', ' ', ' ', ' '};
char arrAliens3[5] = {' ', ' ', ' ', ' ', ' '};
char arrAliens4[5] = {' ', ' ', ' ', ' ', ' '};
char arrAliens5[5] = {' ', ' ', ' ', ' ', ' '};
char arrTempAliens1[5];
char arrTempAliens2[5];
char arrTempAliens3[5];
char arrTempAliens4[5];
char arrTempAliens5[5];
void main(void)
{
//unsigned char ret_val = 0x0F;
unsigned char currKey=0;
// Define some local variables
WDTCTL = WDTPW | WDTHOLD; // Stop watchdog timer
// Useful code starts here
initLeds();
configDisplay();
configKeypad();
while (1) // Forever loop
{
GrClearDisplay(&g_sContext); // Clear the display
while (getKey() != '*')
{
// *** Intro Screen ***
GrStringDrawCentered(&g_sContext, "Space Invaders!", AUTO_STRING_LENGTH, 48, 30, TRANSPARENT_TEXT);
GrStringDrawCentered(&g_sContext, "Press '*' to", AUTO_STRING_LENGTH, 48, 50, TRANSPARENT_TEXT);
GrStringDrawCentered(&g_sContext, "Start the Game", AUTO_STRING_LENGTH, 48, 65, TRANSPARENT_TEXT);
GrFlush(&g_sContext);
}
currKey = getKey();
if (currKey == '*')
{
game = 1;
//turn = 0;
countDown();
while (game == 1)
{
//speedCheck();
generateAliens();
inputCheck();
//displayAliens();
//turn++;
}
GrClearDisplay(&g_sContext);
GrStringDrawCentered(&g_sContext, "You Lose!", AUTO_STRING_LENGTH, 48, 45, TRANSPARENT_TEXT);
GrFlush(&g_sContext);
swDelay(3);
}
} // end while (1)
} //end main
void countDown()
{
//3 Count
GrClearDisplay(&g_sContext);
GrStringDrawCentered(&g_sContext, "3", AUTO_STRING_LENGTH, 48, 45, TRANSPARENT_TEXT);
GrFlush(&g_sContext);
swDelay(3);
GrClearDisplay(&g_sContext);
//2 Count
GrStringDrawCentered(&g_sContext, "2", AUTO_STRING_LENGTH, 48, 45, TRANSPARENT_TEXT);
GrFlush(&g_sContext);
swDelay(3);
GrClearDisplay(&g_sContext);
//1 Count
GrStringDrawCentered(&g_sContext, "1", AUTO_STRING_LENGTH, 48, 45, TRANSPARENT_TEXT);
GrFlush(&g_sContext);
swDelay(3);
GrClearDisplay(&g_sContext);
}
/*void speedCheck()
{
if (turn <= 3)
{
levelSpeed = 10;
}
else if ((turn > 3) && (turn <= 6))
{
levelSpeed = 8;
}
else if ((turn > 6) && (turn <= 9))
{
levelSpeed = 4;
}
else if ((turn > 9) && (turn <= 12))
{
levelSpeed = 2;
}
else
{
levelSpeed = 1;
}
}*/
void generateAliens()
{
memcpy(arrTempAliens1, arrAliens1, 5);
memcpy(arrTempAliens2, arrAliens2, 5);
memcpy(arrTempAliens3, arrAliens3, 5);
memcpy(arrTempAliens4, arrAliens4, 5);
memcpy(arrTempAliens5, arrAliens5, 5);
memcpy(arrAliens1, arrAliens0, 5);
memcpy(arrAliens2, arrTempAliens1, 5);
memcpy(arrAliens3, arrTempAliens2, 5);
memcpy(arrAliens4, arrTempAliens3, 5);
memcpy(arrAliens5, arrTempAliens4, 5);
/*arrTempAliens1 = arrAliens1;
arrTempAliens2 = arrAliens2;
arrTempAliens3 = arrAliens3;
arrTempAliens4 = arrAliens4;
arrTempAliens5 = arrAliens5;
arrAliens1 = arrAliens0;
arrAliens2 = arrTempAliens1;
arrAliens3 = arrTempAliens2;
arrAliens4 = arrTempAliens3;
arrAliens5 = arrTempAliens4;*/
int a = rand() % 4;
if (a == 0)
{
arrAliens0[0] = '0';
arrAliens0[1] = ' ';
arrAliens0[2] = ' ';
arrAliens0[3] = ' ';
arrAliens0[4] = ' ';
}
else if (a == 1)
{
arrAliens0[0] = '0';
arrAliens0[1] = '1';
arrAliens0[2] = ' ';
arrAliens0[3] = ' ';
arrAliens0[4] = ' ';
}
else if (a == 2)
{
arrAliens0[0] = '0';
arrAliens0[1] = '1';
arrAliens0[2] = '2';
arrAliens0[3] = ' ';
arrAliens0[4] = ' ';
}
else if (a == 3)
{
arrAliens0[0] = '0';
arrAliens0[1] = '1';
arrAliens0[2] = '2';
arrAliens0[3] = '3';
arrAliens0[4] = ' ';
}
else if (a == 4)
{
arrAliens0[0] = '0';
arrAliens0[1] = '1';
arrAliens0[2] = '2';
arrAliens0[3] = '3';
arrAliens0[4] = '4';
}
displayAliens();
}
void inputCheck()
{
unsigned char currKey = getKey();
if ((arrAliens5[0] && arrAliens5[1] && arrAliens5[2] && arrAliens5[3] && arrAliens5[4]) == ' ')
{
if (currKey == '0')
{
if ((currKey == '0') && (arrAliens4[0] == '0'))
{
arrAliens4[0] = ' ';
setLeds(0x30);
}
else if ((currKey == '0') && (arrAliens4[0] != '0') && (arrAliens3[0] == '0'))
{
arrAliens3[0]= ' ';
}
else if ((currKey == '0') && (arrAliens4[0] != '0') && (arrAliens3[0] != '0') && (arrAliens2[0] == '0'))
{
arrAliens2[0] = ' ';
}
else if ((currKey == '0') && (arrAliens4[0] != '0') && (arrAliens3[0] != '0') && (arrAliens2[0] != '0') && (arrAliens1[0] == '0'))
{
arrAliens1[0] = ' ';
}
else if ((currKey == '0') && (arrAliens4[0] != '0') && (arrAliens3[0] != '0') && (arrAliens2[0] != '0') && (arrAliens1[0] != '0') && (arrAliens0[0] == '0'))
{
arrAliens0[0] = ' ';
}
}
if (currKey == '1')
{
if ((currKey == '1') && (arrAliens4[1] == '1'))
{
arrAliens4[1] = ' ';
}
else if ((currKey == '1') && (arrAliens4[1] != '1') && (arrAliens3[1] == '1'))
{
arrAliens3[1]= ' ';
}
else if ((currKey == '1') && (arrAliens4[1] != '1') && (arrAliens3[1] != '1') && (arrAliens2[1] == '1'))
{
arrAliens2[1] = ' ';
}
else if ((currKey == '1') && (arrAliens4[1] != '1') && (arrAliens3[1] != '1') && (arrAliens2[1] != '1') && (arrAliens1[1] == '1'))
{
arrAliens1[1] = ' ';
}
else if ((currKey == '1') && (arrAliens4[1] != '1') && (arrAliens3[1] != '1') && (arrAliens2[1] != '1') && (arrAliens1[1] != '1') && (arrAliens0[1] == '1'))
{
arrAliens0[1] = ' ';
}
}
if (currKey == '2')
{
if ((currKey == '2') && (arrAliens4[2] == '2'))
{
arrAliens4[2] = ' ';
}
else if ((currKey == '2') && (arrAliens4[2] != '2') && (arrAliens3[2] == '2'))
{
arrAliens3[2]= ' ';
}
else if ((currKey == '2') && (arrAliens4[2] != '2') && (arrAliens3[2] != '2') && (arrAliens2[2] == '2'))
{
arrAliens2[2] = ' ';
}
else if ((currKey == '2') && (arrAliens4[2] != '2') && (arrAliens3[2] != '2') && (arrAliens2[2] != '2') && (arrAliens1[2] == '2'))
{
arrAliens1[2] = ' ';
}
else if ((currKey == '2') && (arrAliens4[2] != '2') && (arrAliens3[2] != '2') && (arrAliens2[2] != '2') && (arrAliens1[2] != '2') && (arrAliens0[2] == '2'))
{
arrAliens0[2] = ' ';
}
}
if (currKey == '3')
{
if ((currKey == '3') && (arrAliens4[3] == '3'))
{
arrAliens4[3] = ' ';
}
else if ((currKey == '3') && (arrAliens4[3] != '3') && (arrAliens3[3] == '3'))
{
arrAliens3[3]= ' ';
}
else if ((currKey == '3') && (arrAliens4[3] != '3') && (arrAliens3[3] != '3') && (arrAliens2[3] == '3'))
{
arrAliens2[3] = ' ';
}
else if ((currKey == '3') && (arrAliens4[3] != '3') && (arrAliens3[3] != '3') && (arrAliens2[3] != '3') && (arrAliens1[3] == '3'))
{
arrAliens1[3] = ' ';
}
else if ((currKey == '3') && (arrAliens4[3] != '3') && (arrAliens3[3] != '3') && (arrAliens2[3] != '3') && (arrAliens1[3] != '3') && (arrAliens0[3] == '3'))
{
arrAliens0[3] = ' ';
}
}
if (currKey == '4')
{
if ((currKey == '4') && (arrAliens4[4] == '4'))
{
arrAliens4[4] = ' ';
}
else if ((currKey == '4') && (arrAliens4[4] != '4') && (arrAliens3[4] == '4'))
{
arrAliens3[4]= ' ';
}
else if ((currKey == '4') && (arrAliens4[4] != '4') && (arrAliens3[4] != '4') && (arrAliens2[4] == '4'))
{
arrAliens2[4] = ' ';
}
else if ((currKey == '4') && (arrAliens4[4] != '4') && (arrAliens3[4] != '4') && (arrAliens2[4] != '4') && (arrAliens1[4] == '4'))
{
arrAliens1[4] = ' ';
}
else if ((currKey == '4') && (arrAliens4[4] != '4') && (arrAliens3[4] != '4') && (arrAliens2[4] != '4') && (arrAliens1[4] != '4') && (arrAliens0[4] == '4'))
{
arrAliens0[4] = ' ';
}
}
}
else if ((arrAliens5[0] | arrAliens5[1] | arrAliens5[2] | arrAliens5[3] | arrAliens5[4]) != ' ')
{
game = 0;
}
}
void displayAliens()
{
GrClearDisplay(&g_sContext);
GrStringDrawCentered(&g_sContext, arrAliens0, 5, 48, 5, TRANSPARENT_TEXT);
GrStringDrawCentered(&g_sContext, arrAliens1, 5, 48, 25, TRANSPARENT_TEXT);
GrStringDrawCentered(&g_sContext, arrAliens2, 5, 48, 45, TRANSPARENT_TEXT);
GrStringDrawCentered(&g_sContext, arrAliens3, 5, 48, 65, TRANSPARENT_TEXT);
GrStringDrawCentered(&g_sContext, arrAliens4, 5, 48, 85, TRANSPARENT_TEXT);
GrFlush(&g_sContext);
swDelay(3);
}
void swDelay(char numLoops)
{
volatile unsigned int i,j; // volatile to prevent optimization
// by compiler
for (j=0; j<numLoops; j++)
{
i = 50000 ; // SW Delay
while (i > 0) // could also have used while (i)
i--;
}
}
if ((arrAliens5[0] && arrAliens5[1] && arrAliens5[2] && arrAliens5[3] && arrAliens5[4]) == ' ')
This condition is never true because && is a boolean operator and results in either 0 or 1, which can never be equal to the value of the space character.

Struggling with TicTacToe in C

I tried to program a simple TicTacToe in C. The program itselfs compiles and show no errors.
So the program draws the field, reads the names and let a player put his symbol in a specific field. All this happens in a while loop, the should be run until its a draw or someone wins.
But the program just run the loop one time so there is only one turn then the program stops.
So here is my code:
main.c:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "game.h"
#define NAMELENGTH 20
int main(void) {
char names[2][NAMELENGTH];
// field positions
char field[9];
int winner = -1;
getnames(names);
printf("\n\n");
initField(field);
// set field positions to 'empty'
char actualPlayer = (char)(get_random_number()*10.0) % 2;
while (1) {
drawField(field);
turn(actualPlayer, names, field);
winner = isWinner(actualPlayer, field);
drawField(field);
if (winner >= 1) {
printwinner(winner, names);
return 0;
}
else if (winner == 0) {
printDrawGame(names);
return 0;
}
actualPlayer = (actualPlayer + 1) % 2;
}
return 0;
}
game.h Headerfile:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#define NAMELENGTH 20
#pragma warning(disable:4996)
extern void drawField(char *field);
extern void getnames(char nameField[][NAMELENGTH]);
extern void initField(char *field);
extern void turn(char actualPlayer, char names[][NAMELENGTH], char *field);
extern char isWinner(char actualPlayer, char *field);
extern void printwinner(char winnerNumber, char names[][NAMELENGTH]);
extern void printDrawGame(char names[][NAMELENGTH]);
extern double get_random_number();
and then my game.c where I defined my functions:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include "game.h"
#define NAMELENGTH 20
#pragma warning(disable:4996)
void drawField(char *field) {
printf("________________________\n");
printf("| | | |\n");
printf("| %c | %c | %c |\n", field[0], field[1], field[2]);
printf("|_______|_______|______|\n");
printf("| | | |\n");
printf("| %c | %c | %c |\n", field[3], field[4], field[5]);
printf("|_______|_______|______|\n");
printf("| | | |\n");
printf("| %c | %c | %c |\n", field[6], field[7], field[8]);
printf("|_______|_______|______|\n");
}
void getnames(char nameField[][NAMELENGTH]) {
printf("Name of the first player: ");
scanf("%s", nameField[0]);
printf("Name of the second player: ");
scanf("%s", nameField[1]);
}
void initField(char *field) {
for (int i = 0; i <= 8; i++)
{
field[i] = i + '1';
}
}
void turn(char actualPlayer, char names[][NAMELENGTH], char *field) {
char symbol = ' ';
int p1fieldnumber;
int p2fieldnumber;
if (actualPlayer == 0)
{
do {
printf("\nIts Player %s's turn.", names[0]);
char symbol = 'X';
printf("\nNumber of the field which you want to put your symbol in: ");
scanf("%d", &p1fieldnumber);
if (field[p1fieldnumber] == 'X' || field[p1fieldnumber] == 'O')
{
printf("\nField is already occupied!");
p1fieldnumber = 10;
}
} while (p1fieldnumber < 1 || p1fieldnumber > 9);
field[p1fieldnumber-1] = 'X';
}
else {
do {
printf("\nIts Player %s's turn.", names[1]);
char symbol = 'O';
printf("\nNumber of the field which you want to put your symbol in: ");
scanf("%d", &p2fieldnumber);
if (field[p2fieldnumber] == 'X' || field[p2fieldnumber] == 'O')
{
printf("\nField is already occupied!");
p2fieldnumber = 10;
}
} while (p2fieldnumber < 1 || p2fieldnumber > 9);
field[p2fieldnumber-1] = 'O';
}
}
char isWinner(char actualPlayer, char *field) {
char pwinner = '3';
if (((field[0] == 'O') && (field[1] == 'O') && (field[2] == 'O')) ||
(field[3] == 'O') && (field[4] == 'O') && (field[5] == 'O') ||
(field[6] == 'O') && (field[7] == 'O') && (field[8] == 'O') ||
(field[0] == 'O') && (field[4] == 'O') && (field[8] == 'O') ||
(field[2] == 'O') && (field[4] == 'O') && (field[6] == 'O') ||
(field[0] == 'O') && (field[3] == 'O') && (field[6] == 'O') ||
(field[1] == 'O') && (field[4] == 'O') && (field[7] == 'O') ||
(field[2] == 'O') && (field[5] == 'O') && (field[8] == 'O'))
{
pwinner = '2';
}
else if (((field[0] == 'X') && (field[1] == 'X') && (field[2] == 'X')) ||
(field[3] == 'X') && (field[4] == 'X') && (field[5] == 'X') ||
(field[6] == 'X') && (field[7] == 'X') && (field[8] == 'X') ||
(field[0] == 'X') && (field[4] == 'X') && (field[8] == 'X') ||
(field[2] == 'X') && (field[4] == 'X') && (field[6] == 'X') ||
(field[0] == 'X') && (field[3] == 'X') && (field[6] == 'X') ||
(field[1] == 'X') && (field[4] == 'X') && (field[7] == 'X') ||
(field[2] == 'X') && (field[5] == 'X') && (field[8] == 'X'))
{
pwinner = '1';
}
else if (((field[0] == 'X') || (field[0] == 'O')) && ((field[1] == 'X') || (field[1] == 'O')) && ((field[2] == 'X') || (field[2] == 'O')) ||
((field[3] == 'X') || (field[3] == 'O')) && ((field[4] == 'X') || (field[4] == 'O')) && ((field[5] == 'X') || (field[5] == 'O')) ||
((field[6] == 'X') || (field[6] == 'O')) && ((field[7] == 'X') || (field[7] == 'O')) && ((field[8] == 'X') || (field[8] == 'O')) ||
((field[0] == 'X') || (field[0] == 'O')) && ((field[4] == 'X') || (field[4] == 'O')) && ((field[8] == 'X') || (field[8] == 'O')) ||
((field[2] == 'X') || (field[2] == 'O')) && ((field[4] == 'X') || (field[4] == 'O')) && ((field[6] == 'X') || (field[6] == 'O')) ||
((field[0] == 'X') || (field[0] == 'O')) && ((field[3] == 'X') || (field[3] == 'O')) && ((field[6] == 'X') || (field[6] == 'O')) ||
((field[1] == 'X') || (field[1] == 'O')) && ((field[4] == 'X') || (field[4] == 'O')) && ((field[7] == 'X') || (field[7] == 'O')) ||
((field[2] == 'X') || (field[2] == 'O')) && ((field[5] == 'X') || (field[5] == 'O')) && ((field[8] == 'X') || (field[8] == 'O')))
{
pwinner = '0';
}
return pwinner;
}
void printwinner(char winnerNumber, char names[][NAMELENGTH]) {
if (winnerNumber == '1') {
printf("Player %s won!", names[0]);
}
else if (winnerNumber == '2') {
printf("Player %s won!", names[1]);
}
}
void printDrawGame(char names[][NAMELENGTH]) {
printf("Draw!");
}
static int _initialized;
double get_random_number() {
if (!_initialized) {
srand(time(NULL));
_initialized = 1;
}
return (double)rand() / ((double)(RAND_MAX)+1);
}
There are multiple issues
isWinner returns characters '0', '1', '2' and '3'. Replace them with numbers 0, 1, 2, 3, because your are comparing with numbers in main.
In main instead of checking if (winner >= 1) {, which is true even if 3 is returned by isWinner, check for if (winner == 1 || winner == 2) {.
Suggest to use parentheses in isWinner around each condition separated by ||.
Parentheses as shown below for the first condition
if (((field[0] == 'O') && (field[1] == 'O') && (field[2] == 'O')) ||
((field[3] == 'O') && (field[4] == 'O') && (field[5] == 'O')) ||
((field[6] == 'O') && (field[7] == 'O') && (field[8] == 'O')) ||
((field[0] == 'O') && (field[4] == 'O') && (field[8] == 'O')) ||
((field[2] == 'O') && (field[4] == 'O') && (field[6] == 'O')) ||
((field[0] == 'O') && (field[3] == 'O') && (field[6] == 'O')) ||
((field[1] == 'O') && (field[4] == 'O') && (field[7] == 'O')) ||
((field[2] == 'O') && (field[5] == 'O') && (field[8] == 'O')))

Errors in my Tic-Tac-Toe Game

Here's my code for my tic-tac-toe game:
#include <stdio.h>
#include <stdbool.h>
#include <string.h>
int board[3][3] = {
{0, 0, 0},
{0, 0, 0},
{0, 0, 0}
};
int main (void)
{
int const user1 = 1;
int const user2 = 2;
char move[10];
while (! all_locations_filled()) {
printf("User-1, please enter your move:");
scanf("%s", move);
if(valid_location(move)) {
mark_location(user1, move);
display_board(board[3][3]);
}
else if(won_the_game(user1)) {
printf("Congratulations User-1, You Won the Game!");
break;
}
else {
printf("Invalid Move");
}
printf("User-2, please enter your move:");
scanf("%s", move);
if(valid_location(move)) {
mark_location(user2, move);
display_board();
}
else if(won_the_game(user2) {
printf("Congratulations User-2, You Won the Game!");
break;
}
else {
printf("Invalid Move");
}
}
return 0;
}
bool valid_location(char str[10]) {
int strcmp(x, y);
if (strcmp(str[10], "upperLeft") == 0 || strcmp(str[10], "up") == 0 || strcmp(str[10], "upperRight") == 0 || strcmp(str[10], "left") == 0 || strcmp(str[10], "center") == 0 || strcmp(str[10], "right") == 0 || strcmp(str[10], "lowerLeft") == 0 || strcmp(str[10], "down") == 0 || strcmp(str[10], "lowerRight") == 0)
return true;
}
void mark_location(int userU, char str[10]) {
int strcmp(x, y);
if (strcmp(str[10], "upperLeft") == 0)
board[0][0] = userU;
else if (strcmp(str[10], "up") == 0)
board[0][1] = userU;
else if (strcmp(str[10], "upperRight") == 0)
board[0][2] = userU;
else if (strcmp(str[10], "left") == 0)
board[1][0] = userU;
else if (strcmp(str[10], "center") == 0)
board[1][1] = userU;
else if (strcmp(str[10], "right") == 0)
board[1][2] = userU;
else if (strcmp(str[10], "lowerLeft") == 0)
board[2][0] = userU;
else if (strcmp(str[10], "down") == 0)
board[2][1] = userU;
else if (strcmp(str[10], "lowerRight") == 0)
board [2][2] = userU;
}
char display_board(int array[][]) {
int i, j;
for (i=0; i<3; ++i)
for (j=0; j<3; ++j)
if (array[i][j] == 0)
print("-");
else if (array[i][j] == 1)
print("x");
else if (array[i][j] == 2)
print("o");
}
bool all_locations_filled() {
int i, j;
for (i=0; i<3; ++i)
for (j=0; j<3; ++j)
if board[i][j] == 0
return false;
return true;
}
bool won_the_game(userU) {
int i, j;
if (board[0][0] == userU && board[0][1] == userU && board[0][2] == userU)
return true;
else if (board[1][0] == userU && board[1][1] == userU && board[1][2] == userU)
return true;
else if (board[2][0] == userU && board[2][1] == userU && board[2][2] == userU)
return true;
else if (board[0][0] == userU && board[1][0] == userU && board[2][0] == userU)
return true;
else if (board[0][1] == userU && board[1][1] == userU && board[2][1] == userU)
return true;
else if (board[0][2] == userU && board[1][2] == userU && board[2][2] == userU)
return true;
else if (board[0][0] == userU && board[1][1] == userU && board[2][2] == userU)
return true;
else if (board[2][2] == userU && board[1][1] == userU && board[2][0] == userU)
return true;
else
return false;
}
There are a few errors that I don't understand, here they are:
tictactoe.c:50: error: expected expression before ‘}’ token
This error is at the end of the main function but I'm not sure what I did wrong.
tictactoe.c:52: error: nested functions are disabled, use -fnested-functions to re-enable
I didn't know I used a nested function.
tictactoe.c:53: warning: parameter names (without types) in function declaration
This is referring to int strcmp(x, y)
tictactoe.c:55: warning: passing argument 1 of ‘strcmp’ makes pointer from integer without a cast
What did I do wrong with strcmp?
If someone could help me out I'd greatly appreciate it.
You're missing a closing parenthesis here (line #40):
else if(won_the_game(user2) {
Should be:
else if(won_the_game(user2)) {
You have a couple or problems with the strcmp as well.
strcmp(str[10], "upperRight")
The compiler is complaining about the first parameter str[10]. One problem is that this selects a single character from the string, and not the whole string. Another problem is that in an array of size 10, the positions are numbered 0..9 so there isn't even a position 10!
Also, a string literal like "upperRight" contains 10 visible characters plus an extra zero character as a terminator. So it needs 11 positions when stored in the str.

Resources