How to reset array in my tic tac toe game - arrays

I'm new to C and I'm trying to make a tic-tac-toe game for my college project and I'm struggling on how to reset my array in my game.
Every time I play again it does not reset the array. Can anyone help me with this?
Here is the code:
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
char space[3][3] = {
{'1','2','3'},
{'4','5','6'},
{'7','8','9'}
};
void board();
int checkWin();
int game();
void reset();
int main(){
int choice = -1;
do{
printf("\n\n\n\n\n\n\n\t\t\t ================\n");
printf("\t\t\t Tic Tac Toe\n");
printf("\t\t\t ================\n");
printf("\t\t -----------Menu-----------\n\n");
printf("\t\t 1. Play\n");
printf("\t\t 2. Exit\n");
scanf("%d", &choice);
switch(choice){
case 1: game();
break;
case 2: printf("Goodbye!!");
exit(0);
break;
default: printf(".......Wrong Key !.......Try Again!......");
break;
}
}while(choice != 0);
}
int game(){
int player = 1, i, choice;
char mark;
do
{
system("cls");
board();
player = (player % 2) ? 1 : 2;
printf("Player %d, enter a number: ", player);
scanf("%d", &choice);
mark = (player == 1) ? 'X' : 'O';
if (choice == 1)
space[0][0] = mark;
else if (choice == 2)
space[0][1] = mark;
else if (choice == 3)
space[0][2] = mark;
else if (choice == 4)
space[1][0] = mark;
else if (choice == 5)
space[1][1] = mark;
else if (choice == 6)
space[1][2] = mark;
else if (choice == 7)
space[2][0] = mark;
else if (choice == 8)
space[2][1] = mark;
else if (choice == 9)
space[2][2] = mark;
else
{
printf("Invalid move ");
player--;
getch();
}
i = checkWin();
player++;
}while (i == - 1);
board();
reset();
if (i == 1)
printf("==>\aPlayer %d win \n\n", --player);
else
printf("==>\aGame draw\n\n");
getch();
return 0;
}
int checkWin(){
if (space[0][0] == space[0][1] && space[0][1] == space[0][2])
return 1;
else if (space[1][0] == space[1][1] && space[1][1] == space[1][2])
return 1;
else if (space[2][0] == space[2][1] && space[2][1] == space[2][2])
return 1;
else if (space[0][0] == space[1][0] && space[1][0] == space[2][0])
return 1;
else if (space[0][1] == space[1][1] && space[1][1] == space[2][1])
return 1;
else if (space[0][2] == space[1][2] && space[1][2] == space[2][2])
return 1;
else if (space[0][0] == space[1][1] && space[1][1] == space[2][2])
return 1;
else if (space[0][2] == space[1][1] && space[1][1] == space[2][0])
return 1;
else if (space[0][0] != space[0][0] && space[0][1] != space[0][1] && space[0][2] != space[0][2] &&
space[1][0] != space[1][0] && space[1][1] != space[1][1] && space[1][2] != space[1][2] && space[2][0]
!= space[2][0] && space[2][1] != space[2][1] && space[2][2] != space[2][1])
return 0;
else
return - 1;
}
void reset(){
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
space[i][j] = 0;
}
}
}
void board(){
system("cls");
printf("\n\n\tTic Tac Toe\n\n");
printf("Player 1 (X) - Player 2 (O)\n\n\n");
printf(" | | \n");
printf(" %c | %c | %c \n", space[0][0], space[0][1], space[0][2]);
printf("_____|_____|_____\n");
printf(" | | \n");
printf(" %c | %c | %c \n", space[1][0], space[1][1], space[1][2]);
printf("_____|_____|_____\n");
printf(" | | \n");
printf(" %c | %c | %c \n", space[2][0], space[2][1], space[2][2]);
printf(" | | \n\n");
}
I tried using for loop but it does not work; how can I solve this?
void reset(){
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
space[i][j] = 0;
}
}
}

Your reset function is filling the entire 3x3 board matrix with zeros; these represent the nul character, not the character representing the '0' digit. Those nul characters aren't printed (and cannot be), so the board looks 'wrong' after a reset.
However, simply changing the space[i][j] = 0; line to space[i][j] = '0'; won't work! Although you will then initially see a board full of 0 digits, the way your checkWin() function works will then ensure that "Player 1" will have won the new game immediately after their first move, because there will already be rows/columns/diagonals of three identical characters.
So, to get the original ('1' thru '9') display, write your reset() function as below. (Note that the C Standard requires that the characters representing the numerical digits be in order and contiguous, so adding a value of 1 thru 9 to the '0' character will yield the appropriate digit character.)
void reset()
{
int number = 1;
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
space[i][j] = number++ + '0';
}
}
}
Alternatively, you can just declare and initialize a new 'board array' with the data in it, then just use memcpy to rest the space array with the contents of that:
#include <string.h> // For "memcpy"
void reset()
{
char empty[3][3] = { {'1','2','3'}, {'4','5','6'}, {'7','8','9'} };
memcpy(space, empty, sizeof(space));
}
Note, also that all the tests in the following statement are wrong:
else if (space[0][0] != space[0][0] && space[0][1] != space[0][1] && space[0][2] != space[0][2] &&
space[1][0] != space[1][0] && space[1][1] != space[1][1] && space[1][2] != space[1][2] && space[2][0]
!= space[2][0] && space[2][1] != space[2][1] && space[2][2] != space[2][1])
Each one of these is testing if an element is not equal to itself. This can never be true, so that else if block (the return 0; line) is never executed. But you don't actually need it, so you can just delete that block.

Related

How do i repeat this game until ur_choice() >=0

i want to repeat this game until user enters a invalid input other than r/R , p/P or s/S
i created the ur_choice function so that i can get a negative value in case a wrong input is submitted now how do i loop this ur_choice() function until it returns -1
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int ur_choice(){
printf ("choose r/R for Rock p/P for Paper s/S for Scissor : ");
char choice = getchar();
if (choice == 'r' || choice == 'R')
return 0;
if (choice == 'p' || choice == 'P')
return 1;
if (choice == 's' || choice == 'S')
return 2;
else{
return -1;
}
}
int pc_choice(){
srand(time(0));
int random = rand() % 3;
return random;
}
int game(int pc, int you){
if (pc == you)
return -1;
if (pc == 0 && you == 1 || pc == 1 && you == 2 || pc == 2 && you == 0)
return 0;
else if (pc == 0 && you == 2 || pc == 1 && you == 0 || pc == 2 && you == 1)
return 1;
return 2;
}
int main(){
char symbol[3][10] = {"rock","paper","scissor"};
int a,b,result;
a=ur_choice();
if (a>=0){
b=pc_choice();
printf ("You choosed : %s\nPC choosed : %s\n",symbol[a],symbol[b]);
result=game(a,b);
if (result == -1) {
printf("Game Draw!\n");
}
else if (result == 1) {
printf("Wow! You have won the game!\n");
}
else {
printf("Oh! You have lost the game!\n");
}
}
else{
printf("Invalid choice!\n");
exit(0);
}
}
When you want to repeat something, you can use a while statement.
Hence, you can assign a value to your variable a and check whether it returns -1 or not at the same time using this syntax.
while ((a = ur_choice()) != -1) {
// game's content
}

i am making a tic tac toe game in c, the wincheck function but in the code below is not working

I am making a tic tac toe game in c, the wincheck function in the code below is not working, if there is a win, the game doesn't stop, it keeps running seems that the wincheck function doesn't make the wincheckvalue 1,but why?
#include <stdio.h>
#include <stdbool.h>
char list[10] = {'0',' ',' ',' ',' ',' ',' ',' ',' ',' '};
void displayboard()
{
printf("\n | | ");
printf("\n %c | %c | %c ",list[7],list[8],list[9]);
printf("\n | | \n");
printf("--------------------\n");
printf("\n | | ");
printf("\n %c | %c | %c ",list[4],list[5],list[6]);
printf("\n | | \n");
printf("--------------------\n");
printf("\n | | ");
printf("\n %c | %c | %c ",list[1],list[2],list[3]);
printf("\n | | \n");
}
int wincheck(char c)
{
int returnvalue = 0;
if (list[1] == list[2] == c && list[2] == list[3] == c)
{
returnvalue = 1;
}
else if (list[4] == list[5] == c && list[5] == list[6] == c)
returnvalue = 1;
else if (list[7] == list[8] == c && list[8] == list[9] == c)
returnvalue = 1;
else if (list[1] == list[4] == c && list[4] == list[7] == c)
returnvalue = 1;
else if (list[2] == list[5] == c && list[5] == list[8] == c)
returnvalue = 1;
else if (list[3] == list[6] == c && list[6] == list[9] == c)
returnvalue = 1;
else if (list[1] == list[5] == c && list[5] == list[9] == c)
returnvalue = 1;
else if (list[3] == list[5] == c && list[5] == list[7] == c)
returnvalue = 1;
return returnvalue;
}
int main()
{
int wincheckvalue;
int v = 0;
int a;
int b;
while (true)
{
displayboard();
printf("Player 1(X) ,Input num: ");
scanf("%i",&a);
while (list[a] == '0' || list[a] == 'X' )
{
displayboard();
printf("Invalid move!\n");
printf("Input num: ");
scanf("%i",&a);
}
list[a] = 'X';
wincheckvalue = wincheck('X');
if (wincheckvalue == 1)
{
printf("Player 1 has won!!");
break;
}
displayboard();
printf("Player 2(0) ,Input num: ");
scanf("%i",&b);
while (list[b] == 'X' || list[b] == '0')
{
displayboard();
printf("Invalid move!\n");
printf("Input num: ");
scanf("%i",&b);
}
list[b] = '0';
displayboard();
wincheckvalue = wincheck('0');
if (wincheckvalue == 1)
{
printf("Player 2 has won!!");
break;
}
}
}
All your comparisons like list[1] == list[2] == c are wrong.
That comparison is equal to (list[1] == list[2]) == c, which means you compare the true/false (the integers 1 or 0 respectively) result of list[1] == list[2] to the value of c. This will very likely never be true.
You must split your comparison like list[1] == c && list[2] == c etc.

I have a problem with this how can I write multiple characters inside an array in c please get me a solution

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int generaterandomno(int n)
{
srand(time(NULL));
return rand() % n;
}
int greater(char char1, char char2)
{
//For Rock, Paper, Scissors - Returns 1 if c1>c2 and 0 otherwise. If c1 == c2 it will return -1
if (char1 == char2)
{
return -1;
}
else if ((char1 == 'r') && (char2 == 's'))
{
return 1;
}
else if ((char2 == 'r') && (char1 == 's'))
{
return 0;
}
else if ((char1 == 'p') && (char2 == 'r'))
{
return 1;
}
else if ((char2 == 'p') && (char1 == 'r'))
{
return 0;
}
else if ((char1 == 's') && (char2 == 'p'))
{
return 1;
}
else if ((char2 == 's') && (char1 == 'p'))
{
return 0;
}
}
int main()
{
int playerscore = 0, compscore = 0, temp;
char playerchar, compchar;
char dict[] = {'r', 'p', 's'};
printf("Welcome to The Rock, Paper, Scissors Game, I hope you will enjoy\n");
for (int i = 0; i < 3; i++)
{
// Take Player 1's input
printf("Player 1:\n");
printf("Choose 1 for Rock, 2 for Paper and 3 for Scissors\n");
scanf("%d", &temp);
getchar();
playerchar = dict[temp - 1];
printf("You chose %c\n\n", playerchar);
// Generate computer's input
printf("Computer's Turn:\n");
printf("Choose 1 for Rock, 2 for Paper and 3 for Scissors\n");
temp = generaterandomno(3) + 1;
compchar = dict[temp - 1];
printf("Computer chose %c\n\n", compchar);
// Compare the scores
if (greater(compchar, playerchar) == 1)
{
compscore += 1;
printf("Computer got it!!!\n");
}
else if (greater(compchar, playerchar) == -1)
{
compscore += 1;
playerscore += 1;
printf("It's a Draw!!!\n");
}
else
{
playerscore += 1;
printf("You got it!!!\n");
}
printf("You: %d\nComp: %d\n\n",playerscore, compscore);
}
if (playerscore > compscore)
{
printf("----------------------\n");
printf("------You Won!!!------\n");
printf("----------------------\n");
}
else if (compscore > playerscore)
{
printf("---------------------\n");
printf("---Computer Won!!!---\n");
printf("---------------------\n");
}
else
{
printf("----------------------\n");
printf("----It's a Draw!!!----\n");
printf("----------------------\n");
}
return 0;
}
char dict[] = {'r', 'p', 's'};
I have a problem with this 👆 how can I write multiple characters inside an array in c please get me a solution. I want to write rock paper and scissors replacing this please get a solution for me.....
I think what you are asking is about outputting 'rock' instead of 'r' when confirming the choice.
You could create a 2 dimensional array to hold the strings instead of a one dimensional array. For example...
char rps[][] = { "rock", "paper", "scissors" };
This would allow you to get at both the initials for example...
rps[0][0] would be 'r'
or the whole string...
rps[0] is "rock"
The important thing to note is that a string is just a char array with a null terminator.
These links will help explain a bit more.
https://www.programiz.com/c-programming/c-strings
https://www.codingame.com/playgrounds/14213/how-to-play-with-strings-in-c/array-of-c-string

Decreasing EXE Size

I wrote a TicTacToe program in C that reads 68.3 KB on my hard disk, and I was wondering if there are any sort of optimization techniques I could use to decrease the amount of bytes in my .exe file.
For instance, is there anything in my code that I could change to make the resulting executable "smaller"?
Code:
#include <stdio.h>
#include <stdbool.h>
void drawBoard(int [3][3]);
bool boardFull(int [3][3]);
bool checkWin(int [3][3], int);
int main()
{
int board[3][3];
int marker, turn, row, column, i, j;
for(i = 0; i < 3; i++)
{
for(j = 0; j < 3; j++)
board[i][j] = -1;
}
printf("Player One is X\n");
printf("Player Two is O\n");
drawBoard(board);
while(!boardFull(board))
{
if((turn % 2) == 0)
{
printf("Player One's turn.\n");
marker = 1;
}
else
{
printf("Player Two's turn.\n");
marker = 0;
}
printf("Which row? ");
scanf("%i", &row);
printf("Which column? ");
scanf("%i", &column);
if(board[row - 1][column - 1] != -1)
{
while(board[row - 1][column - 1] != -1)
{
printf("Space already taken. Try again.\n");
printf("Which row? ");
scanf("%i", &row);
printf("Which column? ");
scanf("%i", &column);
printf("\n");
}
}
board[row - 1][column - 1] = marker;
drawBoard(board);
if(checkWin(board, marker))
{
if(marker == 1)
printf("Player One wins!!");
else
printf("Player Two wins!!");
return 0;
}
turn++;
}
printf("Cat's game!\n");
return 0;
}
void drawBoard(int board[3][3])
{
int i, j;
printf("\n");
for(i = 0; i < 3; i++)
{
printf(" ");
for(j = 0; j < 3; j++)
{
if(board[i][j] == 0)
printf("O");
else if(board[i][j] == 1)
printf("X");
else
printf(" ");
if(j == 2)
continue;
else
printf(" | ");
}
if(i == 2)
printf("\n\n");
else
printf("\n -----------\n");
}
}
bool boardFull(int board[3][3])
{
int i, j;
for(i = 0; i < 3; i++)
{
for(j = 0; j < 3; j++)
{
if(board[i][j] == -1)
return false;
}
}
return true;
}
bool checkWin(int board[3][3], int w)
{
return
board[0][0] == w && board[1][1] == w && board[2][2] == w
|| board[0][2] == w && board[1][1] == w && board[2][1] == w
|| board[0][0] == w && board[1][0] == w && board[2][0] == w
|| board[0][1] == w && board[1][1] == w && board[2][1] == w
|| board[0][2] == w && board[1][2] == w && board[2][2] == w
|| board[0][0] == w && board[0][1] == w && board[2][2] == w
|| board[1][0] == w && board[1][1] == w && board[2][2] == w
|| board[2][0] == w && board[2][1] == w && board[2][2] == w;
}

why does my printf statement pop up simultaneously? and get an infinite loop?

Why is it that when I start the program, I will start by doing the first printf statement, and then i input, then it simultaneously does two printf statements.
Is this what's causing the infinite loop as well?
Starting the program
Player 1: Choose your symbol:
a
This part, they both output simultaneously
Player 2: Choose your symbol:
player1, enter placement:
And then I get an infinite loop. Is it due to the simultaneous output?
code
include <stdio.h>
int check(char player);
void move(char player);
char board[3][3] ;
int main(void)
{
int first;
char player1, player2;
printf("Player 1: Choose your symbol: \n");
player1 = getchar();
printf("Player 2: Choose your symbol: \n");
player2 = getchar();
int i=0;
int win;char turn;
while(win == 0)
{
if((i%2) == 0){
turn = player1;
move(player1);
win = check(player1);
print();}
else {
turn = player2;
move(player2);
win = check(player2);
print();}
i++;
}
if (i == 8)
printf("its a tie");
else
printf("the winner is %c", turn);
return 0;
}
/*printing the board that takes in a placement int*/
void print(void)
{
int r;
printf("\n");
for (r = 0; r < 3; r++){
printf(" %c | %c | %c \n" , board[r][0], board[r][2], board[r][3]);
if (r != 2)
printf("___________\n");
}
printf("\n");
return;
}
/*check to see if someone won*/
int check(char player)
{
int r, c;
for ( r = 0 ; r <3 ; r++)
{
if ((board[r][0] == player) && (board[r][1] == player) && (board[r][2] == player))
return 1;
}
for ( c = 0 ; c <3 ; c++)
{
if ((board[0][c] == player) && (board[1][c] == player) && (board[2][c] == player))
return 1;
}
if((board[0][0] == player) && (board[1][1] == player) && (board[2][2] == player))
return 1;
if((board[0][2] == player) && (board[1][1] == player) && (board[2][0] == player))
return 1;
return 0;
}
void move(char player)
{
int place;
printf("player1, enter placement: \n");
scanf("%d", &place);
if (place == 1)
board[0][0] = player;
else if (place == 2)
board[0][1] = player;
else if (place == 3)
board[0][2] = player;
else if (place == 4)
board[1][0] = player;
else if (place == 5)
board[1][1] = player;
else if (place == 6)
board[1][2] = player;
else if (place == 7)
board[2][0] = player;
else if (place == 8)
board[2][1] = player;
else if (place == 9)
board[2][2] = player;
}
Because first getchar() leaves a newline character in the input stream which consumed by the subsequent getchar(). One way is to use another getchar() to consume the unwanted newline chars.
printf("Player 1: Choose your symbol: \n");
player1 = getchar();
getchar(); // consume a newline
printf("Player 2: Choose your symbol: \n");
player2 = getchar();
getchar(); // consume a newline
You're probably getting the '\n' character from getchar();
You could do something like:
printf("Player 2: Choose your symbol: \n");
player2 = getchar();
while ( player2 != '\n' ) { player2 = getchar(); }
to clear standard input before reading again with the next getchar();

Resources