How to print the result of a function in C? - c

I'm currently working on a Tic Tac Toe game that will be interactive for a user. I think I have it all working, I just can't figure out how to get the displayBoard() function to print out the current state of the board so that the user knows which spaces are open and which spaces are not. How can I get the displayBoard() function to print to the screen?
My code is as follows:
#include <stdio.h>
void initializeBoard(char board[][3]);
void displayBoard(char board[][3]);
void makeMove(char board[][3], int player);
int main(void)
{
char board[3][3];
int row;
int column;
int move = 1;
int player = 1;
initializeBoard(board);
while (move > 10)
{
displayBoard(board);
makeMove(board, player);
move++;
if (move == 2 || move == 4 || move == 6 || move == 8)
{
player = 2;
}//close of if
else
{
player = 1;
}//close of else
}
}//close of main function
void initializeBoard(char board[][3])
{
int i = 0;
int j = 0;
while (i < 4)
{
while (j < 4)
{
board[i][j] = ' ';
j++;
}//close of while loop
j = 0;
i++;
}//close of while loop
}//close of initializeBoard
void displayBoard(char board[][3])
{
printf(" 1 2 3");
printf("1 [%c] [%c] [%c]", board[1][1],board[1][2],board[1][3]);
printf("2 [%c] [%c] [%c]", board[2][1],board[2][2],board[2][3]);
printf("3 [%c] [%c] [%c]", board[3][1],board[3][2],board[3][3]);
}//close of displayBoard
void makeMove(char board[][3], int player)
{
int row;
int column;
printf("Enter the row and column you'd like to fill:");
scanf("%d %d", &row, &column);
if (player == 1)
{
board[row][column] = 'X';
}//close of if
else
{
board[row][column] = 'O';
}//close of else
}//close of makeMove

you have written many things wrong, nor you have called any function to draw board in main(). Here, I have a better version of the game. You can see it and compare and make yours better. its pretty similar, its in C++ but if thats a problem, just change input and output lines. PS: i dont remember much of C
goodluck.
#include<iostream.h>
#include<conio.h>
//to print digits on board and for checks
char num[10]={'0','1','2','3','4','5','6','7','8','9'};
void gameBoard() //function to print board
{
cout<<" | | "<<endl;
cout<<" "<<num[1]<<" | "<<num[2]<<" | "<<num[3]<<" "<<endl;
cout<<"_____|_____|_____"<<endl;
cout<<" | | "<<endl;
cout<<" "<<num[4]<<" | "<<num[5]<<" | "<<num[6]<<" "<<endl;
cout<<"_____|_____|_____"<<endl;
cout<<" | | "<<endl;
cout<<" "<<num[7]<<" | "<<num[8]<<" | "<<num[9]<<" "<<endl;
cout<<" | | "<<endl;
}
int win() //function to check if a player wins or game is draw,
// returns 1 if someone won, 0 if draw and -1 is game is still going on
{
if(num[1]==num[2] && num[2]==num[3])
return 1;
else if(num[4]==num[5] && num[5]==num[6])
return 1;
else if(num[7]==num[8] && num[8]==num[9])
return 1;
else if(num[1]==num[4] && num[4]==num[7])
return 1;
else if(num[2]==num[5] && num[5]==num[8])
return 1;
else if(num[3]==num[6] && num[6]==num[9])
return 1;
else if(num[1]==num[5] && num[5]==num[9])
return 1;
else if(num[3]==num[5] && num[5]==num[7])
return 1;
else if(num[1]!='1' && num[2]!='2' && num[3]!='3' && num[4]!='4' && num[5]!='5' && num[6]!='6' && num[7]!='7' && num[8]!='8' && num[9]!='9')
return 0;
else
return -1;
}
void input() //function for player input and to assign both players signs.
{
int choice,w,player=1;
char sym;
do{
clrscr();
gameBoard();
player=(player%2)? 1:2; //to check whos turn
cout<<"\nPlayer "<<player<< " ,Enter Your Choice: ";
cin>>choice;
sym = (player == 1)? 'X' : 'O'; //assigns X to player 1 and O to player 2
if(choice==1&&num[1]=='1')
num[1]=sym;
else if(choice==2&&num[2]=='2')
num[2]=sym;
else if(choice==3&&num[3]=='3')
num[3]=sym;
else if(choice==4&&num[4]=='4')
num[4]=sym;
else if(choice==5&&num[5]=='5')
num[5]=sym;
else if(choice==6&&num[6]=='6')
num[6]=sym;
else if(choice==7&&num[7]=='7')
num[7]=sym;
else if(choice==8&&num[8]=='8')
num[8]=sym;
else if(choice==9&&num[9]=='9')
num[9]=sym;
else
{
cout<<"\n\nWrong Move, Please enter again:";
cin.ignore();
player--; //Ignores the input and let player choose again.
getch();
}
w=win();
player++;
}while(w==-1);
clrscr();
gameBoard();
if(w==1)
cout<<"\n Player "<<--player<<" wins!!";
else
cout<<"\n Game Draw";
getch();
}
int main()
{
clrscr();
input(); return 0;
}

Related

making a random move in tic tac toe -in C

I am doing a tic tac toe game in which the user competes against the computer. When the user chooses a spot, the computer should also make a move. However, I can't seem to get that to work. Whenever the user makes a move, it instantly asks me to make another one instead of filling one of the spots with the computer symbol (O)
I did the computer part in a different function from the user's (AI() and User()) and tried calling it inside each "else if" statement of the switch, however, that only keeps the game blinking and doesn't allow me to continue.
EDIT: I also tried to, instead of making it a function, do the whole AI() process inside "else if" statements, but it only seemed to work when the user chose 1, not for the rest of the options.
EDIT #2: I changed (k = 1) to (k == 1) in the code.
To make the following code easier to read I made it 1x3 instead of 3x3:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
typedef struct symbol{
int marked;
char symbol;
} SPOT;
SPOT spot1 = {0,'1'};
SPOT spot2 = {0,'2'};
SPOT spot3 = {0,'3'};
char symbol = 'X';
char compu = 'O';
void table();
int Check();
void User();
void AI();
int main(){
system("cls");
User();
AI();
Check();
return 0;
}
void table(){
printf("\n %c | %c | %c ",spot1.symbol,spot2.symbol,spot3.symbol);
}
void User(){
char choice;
do{
loop++;
do{
table();
printf("\n\nChoose a spot: ");
fflush(stdin);
scanf("%c",&choice);
} while(choice < '1' || choice > '3');
switch(choice){
case '1':
if(choice == '1'){
system("cls");
if(spot1.marked == 1){
printf("\nThat spot is taken\n");
}
else if(spot1.marked == 0){
spot1.marked = 1;
spot1.symbol = symbol;
}
}
break;
case '2':
if(choice == '2'){
system("cls");
if(spot2.marked == 1){
printf("\nThat spot is taken\n");
}
else if(spot2.marked == 0){
spot2.marked = 1;
spot2.symbol = symbol;
}
}
break;
case '3':
if(choice == '3'){
system("cls");
if(spot3.marked == 1){
printf("\nThat spot is taken\n");
}
else if(spot3.marked == 0){
spot3.marked = 1;
spot3.symbol = symbol;
}
}
break;
}while(Check() != 0 && Check() != 1);
}
void AI(){
int random,k;
srand(time(NULL));
do{
random = rand() % 4;
k = 0;
if(spot1.symbol == symbol || spot2.symbol == symbol || spot3.symbol == symbol){
k = 1;
}
}while(k == 1);
switch(random){
case '1':
if (random == 1){
spot1.symbol = compu;
spot1.marked = 1;
}
break;
case '2':
if (random == 2){
spot2.symbol = compu;
spot2.marked = 1;
}
break;
case '3':
if (random == 3){
spot3.symbol = compu;
spot3.marked = 1;
}
break;
}
}
int Check() {
if(spot1.marked == spot2.marked && spot2.marked == spot3.marked){
if(spot1.symbol == symbol){
return 1;
printf("You won!");
}
else if(spot1.symbol == compu){
return 0;
printf("You lost!");
}
}
else {
return 2;
}
}
That's because each function in your main is called one after another.
Your program is stuck in the User() call
Try changing your main to :
int main(){
system("cls");
do{
User();
AI();
}while(Check() != 0 && Check() != 1);
return 0;
}
Removing the do{...}while(Check() != 0 && Check() != 1); in the User function
and change while(k = 1); to while(k == 1);

Beginner C program- Segmentation fault (core dumped)

The program is compiling just fine but I keep receiving an output of:
Segmentation fault (code dumped)
The program is designed to play a simple game of tic tac toe with a human and computer that plays a random space. I'm curious as to what this message means and feel it has something to do with my pointers but I'm not sure how or where. Any help is very appreciated. Here's my code:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
// Initializing board
void clear_table(char board [3][3])
{
int i, j;
//Outer loop for rows
for(i=0; i<3; i++)
{
//Inner loop for columns
for(j=0; j<3; j++)
{
board[i][j] = ' ';
}
}
}
// prints the board in nice format
void display_table (char board [3][3])
{
int i, j;
printf("\n\n The board is: \n");
//Outer loop for rows
for(i=0; i<3; i++)
{
//Inner loop for columns
for(j=0; j<3; j++)
{
//Printing character
printf(" %c", board[i][j]);
}
printf(" ");
//Printing footer
printf("\n _ _ _ \n");
}
}
//Validates the move
int check_legal_option(char board[3][3], int row, int col)
{
//Checking position
if(board[row][col] == ' ')
{
//Available
return 1;
}
return 0;
}
//Function that generates a valid move for player 2
void generate_player2_move(char board[3][3], int *row, int *col)
{
//Validating move
while(1)
{
//Generating computer position
*row = rand() % 3;
*col = rand() % 3;
//Checking for empty position
if(check_legal_option(board, *row, *col) == 1)
break;
}
}
//Function that checks whether board is full or not
int check_table_full(char board[3][3])
{
int i, j;
//Outer loop for rows
for(i=0; i<3; i++)
{
//Inner loop for columns
for(j=0; j<3; j++)
{
//Board is not Full
if(board[i][j] == ' ')
return 0;
}
}
//Board Full
return 1;
}
// win returns true if the given player has won on the
// given board, else it returns false
int win (char board [3][3], char player)
{
return (board[0][0] == player && board[0][1] == player && board[0][2] == player) ||
(board[1][0] == player && board[1][1] == player && board[1][2] == player) ||
(board[2][0] == player && board[2][1] == player && board[2][2] == player) ||
(board[0][0] == player && board[1][0] == player && board[2][0] == player) ||
(board[0][1] == player && board[1][1] == player && board[2][1] == player) ||
(board[0][2] == player && board[1][2] == player && board[2][2] == player) ||
(board[0][0] == player && board[1][1] == player && board[2][2] == player) ||
(board[0][2] == player && board[1][1] == player && board[2][0] == player);
}
//Function that checks for winner
int check_three_in_a_row(char board[3][3], char player1, char player2)
{
//Checking for winning of player
if(win(board, 'X') == 1 && win(board, 'O') == 1)
{
return 2;
}
//Checking for winning of player 1
else if(win(board, 'O') == 1)
{
return 0;
}
//Checking for winning of player 2
else if(win(board, 'X') == 1)
{
return 1;
}
//Tie
else
{
return -1;
}
}
//Function that plays the game
void playGame(char board[3][3], char player1, char player2)
{
int row, col;
int winner;
//Loop till board is full
while(check_table_full(board) != 1)
{
//Player turn
while(1)
{
//Reading positions from user
printf("\n Player 1 enter your selection [row,col]: ");
scanf("%d %d", &row, &col);
//Making suitable for array indexing
row = row - 1;
col = col - 1;
//Checking for empty position
if(check_legal_option(board, row, col) == 1)
break;
else
printf("\n Invalid choice... Try again!! \n ");
}
//Storing in array
board[row][col] = player1;
//Printing board
display_table(board);
//Finding winner
winner = check_three_in_a_row(board, player1, player2);
//If either of winner is found
if(winner >= 0 && winner <= 2)
{
//Printing winner
switch(winner)
{
//Displaying winner
case 0: printf("\n Player 1 won the game... \n"); break;
case 1: printf("\n Player 2 won the game... \n"); break;
case 2: printf("\n Game Tie ... \n"); break;
}
return;
}
//Generating a move
generate_player2_move(board, &row, &col);
//Storing in array
board[row][col] = player2;
printf("\n Player 2 has entered [row,col]: %d,%d \n", row+1, col+1);
//Printing board
display_table(board);
//Finding winner
winner = check_three_in_a_row(board, player1, player2);
//If either of winner is found
if(winner >= 0 && winner <= 2)
{
//Printing winner
switch(winner)
{
//Displaying winner
case 0: printf("\n Player 1 won the game... \n"); break;
case 1: printf("\n Player 2 won the game... \n"); break;
case 2: printf("\n Game Tie ... \n"); break;
}
return;
}
}
}
//Main function
int main()
{
//Board
char board[3][3];
//Player characters
char player1Character='O', player2Character='X';
//Initializing random function
srand(time(NULL));
//Initializing board
clear_table(board);
//Printing board
display_table(board);
//Playing game
playGame(board, player1Character, player2Character);
return 0;
}
The problem is right here, at the start of the PlayGame function:
//Reading positions from user
printf("\n Player 1 enter your selection [row,col]: ");
scanf("%d %d", &row, &col);
The prompt seems to suggest that you should enter two numbers separated by a comma, like 2,2 to get the center position.
However, the format string scanf("%d %d" doesn't say anything about a comma, so the input fails and col never gets a value.

Struggles with TicTacToe

I have written a program in C, that when runs, is supposed to allow the user to play a game of tic tac toe - or noughts and crosses, whether it be against the computer, or another user. I have nearly completed it, and it prints the grid out perfectly fine. However, when I go to input which row/column I want to place the symbol in, instead of inserting the symbol, it just removes a space in the grid, aligning it incorrectly. I have tried to put a loop in the main method which fills the grid in with spaces to try and prevent null values.
If anyone could suggest anything it would be much appreciated. Thanks in advance!
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void user_prompt();
void clear_board(char gameBoard[3][3]);
void display_board(char gameboard[3][3]);
void user_move(char gameBoard[3][3]);
void computer_move(char gameBoard[3][3]);
int detect_win(char gameBoard[3][3]);
int gameMode;
char symbol1;
char symbol2;
char nickname1[10];
char nickname2[10] = "TicTacBot";
char gameBoard[3][3];
char playerTurn[10];
int main(void){
int i, j;
char gameBoard [3][3];
clear_board(gameBoard);
for(int i = 0; i < 3; i++) {
for(int j = 0; j < 3; j++) {
gameBoard[i][j] = ' ';
}
}
i = 0;
j = 0;
user_prompt();
display_board(gameBoard);
if(gameMode == 2){
for(i = 0; i < 9; i++){
user_move(gameBoard);
display_board(gameBoard);
if(detect_win(gameBoard) == 1){
printf("The winner is %s!!!\n", nickname1);
}
if(detect_win(gameBoard) == 2){
printf("The winner is %s!!!\n", nickname2);
}
}
}
else{
user_move(gameBoard);
display_board(gameBoard);
for(i = 0; i < 4; i++){
printf("TicTacBot's move.\n");
computer_move(gameBoard);
display_board(gameBoard);
if(detect_win(gameBoard) == 1){
printf("The winner is %s!!!\n", nickname1);
}
user_move(gameBoard);
display_board(gameBoard);
if(detect_win(gameBoard) == 1){
printf("The winner is %s!!!\n", nickname2);
}
}
}
if(detect_win(gameBoard) == 0){
("The game ended in stalemate!\n");
}
return 0;
}
/*
Prompts the user to enter a nickname, what symbol they would like to use, and whether they'd rather play against the computer or another user.
*/
void user_prompt(void){
printf("Please choose whether you would rather play against the computer (enter '1'),or another user (enter '2'): \n");
scanf("%d", &gameMode);
getchar();
while(gameMode != 1 && gameMode != 2){
printf("Please enter a valid digit: \n");
scanf("%d", &gameMode);
getchar();
}
if(gameMode == 1){
printf("Please choose whether you would rather play as 'X' (enter 'x') or as 'O' (enter 'o'): \n");
scanf(" %c", &symbol1);
getchar();
if(symbol1 == 'x') {
symbol2 == 'o';
}
else{
symbol2 == 'x';
}
}
else{
printf("Player 1, would you like to play as 'X' (enter 'x') or as 'O' (enter 'o'): \n");
scanf(" %c", &symbol1);
getchar();
while(symbol1 != 'x' && symbol1 != 'o'){
printf("Please enter a valid symbol: \n");
scanf(" %c", &symbol1);
getchar();
}
if(symbol1 == 'x'){
symbol2 == 'o';
}
else{
symbol2 == 'x';
}
}
if(gameMode == 1){
printf("Please enter a nickname: \n");
fgets(nickname1, 10, stdin);
getchar();
}
else{
printf("Please enter a nickname for player 1: \n");
fgets(nickname1, 10, stdin);
getchar();
printf("Please enter a nickname for player 2: \n");
fgets(nickname2, 10, stdin);
getchar();
}
}
/*
Resets the game board.
*/
void clear_board(char gameBoard[3][3]){
for(int i = 0; i < 3; i++){
for(int j = 0; j < 3; j++){
gameBoard[i][j] = 0;
}
}
}
/*
Displays the game board and all symbols within it.
*/
void display_board(char gameBoard[3][3]){
for(int i = 0; i < 3; i++){
for(int j = 0; j < 3; j++){
printf(" %c ", gameBoard[i][j]);
if(j == 2){
printf("\n");
}
if(j != 2){
printf("|");
}
}
if(i != 2){
printf("---+---+---\n");
}
}
}
/*
Takes input of a position on the board from the user, then places the user's symbol into that space on the game board.
*/
void user_move(char gameBoard[3][3]){
int row;
int column;
if(playerTurn == nickname1){
printf("Would you like to enter your %c in row 1, 2 or 3? \n", symbol1);
scanf("%d", &row);
getchar();
printf("Would you like to enter your %c in column 1, 2 or 3? \n", symbol1);
scanf("%d", &column);
getchar();
}
else{
printf("Would you like to enter your %c in row 1, 2 or 3? \n", symbol2);
scanf("%d", &row);
getchar();
printf("Would you like to enter your %c in column 1, 2 or 3? \n", symbol2);
scanf("%d", &column);
getchar();
}
if(row < 1 || row > 3){
printf("Please enter a valid row number: \n");
scanf("%d", &row);
getchar();
}
if(column < 1 || column > 3){
printf("Please enter a valid column number: \n");
scanf("%d", &row);
getchar();
}
if(gameBoard[row-1][column-1] != ' '){
printf("The position you entered is already taken. Try again! \n");
display_board(gameBoard);
user_move(gameBoard);
}
else if(gameBoard[row-1][column-1] != ' '){
printf("The position you entered is already taken. Try again! \n");
display_board(gameBoard);
user_move(gameBoard);
}
else{
if(playerTurn == nickname1){
gameBoard[row-1][column-1] = symbol1;
}
else{
gameBoard[row-1][column-1] = symbol2;
}
}
printf("%c", symbol2);
if(gameMode == 2){
return;
}
if(strcmp(playerTurn, nickname1)==0){
strcpy(playerTurn, nickname2);
}
else if(strcmp(playerTurn, nickname2)==0){
strcpy(playerTurn, nickname1);
}
}
/*
Automates a strategic move from the computer, aiming to win the game, or at the least prevent the user from winning
*/
void computer_move(char gameBoard[3][3]){
int row;
int column;
int endTurn = 0;
row = rand() % 3 + 0;
column = rand() % 3 + 0;
if(gameBoard[row][column] != symbol1 && gameBoard[row][column] != symbol2){
gameBoard[row][column] = symbol2;
endTurn = 1;
}
}
/*
Detects a win on the game board. Checks if there are three identical symbols in a row, or if there are no more spaces on the game board.
*/
int detect_win(char gameBoard[3][3]){
for(int row = 0; row < 3; row++){
if(gameBoard[row][0] == gameBoard[row][1] && gameBoard[row][1] == gameBoard[row][2]){
if(gameBoard[row][0] == symbol1){
return 1;
}
if(gameBoard[row][0] == symbol2){
return 2;
}
}
}
for(int column = 0; column < 3; column++){
if(gameBoard[0][column] == gameBoard[1][column] && gameBoard[1][column] == gameBoard[2][column]){
if(gameBoard[0][column] == symbol1){
return 1;
}
if(gameBoard[0][column] == symbol2){
return 2;
}
}
}
if(gameBoard[0][0] == gameBoard[1][1] && gameBoard[1][1] == gameBoard[2][2]){
if(gameBoard[1][1] == symbol1){
return 1;
}
if(gameBoard[1][1] == symbol2){
return 2;
}
}
if(gameBoard[0][2] == gameBoard [1][1] && gameBoard[1][1] == gameBoard[2][0]){
if(gameBoard[1][1] == symbol1){
return 1;
}
if(gameBoard[1][1] == symbol2){
return 2;
}
}
return 0;
}
There's a small logical error:
In line 98, 101, 117, and 121, (and perhaps other places - please check) you have used == (operator for equality check) instead of the = used for assignment.
For example,
symbol2 == 'o';
should be replaced with
symbol2 = 'o';
Hence, it just checks for equality, throws away the result, and continues; with no changes made to symbol2.

MiniMax algorithm tic-tac-toe in C explanation

trying to learn about computer game players to familiarise myself with AI. I understand how minimax works in theory but cant get my head around how this code I found online works.
can someone explain the minimax fruction/computer move function (lines 38-78)to me.
credit: https://gist.github.com/MatthewSteel/3158579
thanks
char gridChar(int i) {
switch(i) {
case -1:
return 'X';
case 0:
return ' ';
case 1:
return 'O';
}
}
void draw(int b[9]) {
printf(" %c | %c | %c\n",gridChar(b[0]),gridChar(b[1]),gridChar(b[2]));
printf("---+---+---\n");
printf(" %c | %c | %c\n",gridChar(b[3]),gridChar(b[4]),gridChar(b[5]));
printf("---+---+---\n");
printf(" %c | %c | %c\n",gridChar(b[6]),gridChar(b[7]),gridChar(b[8]));
}
int win(const int board[9]) {
//determines if a player has won, returns 0 otherwise.
unsigned wins[8][3] = {{0,1,2},{3,4,5},{6,7,8},{0,3,6},{1,4,7},{2,5,8},{0,4,8},{2,4,6}};
int i;
for(i = 0; i < 8; ++i) {
if(board[wins[i][0]] != 0 &&
board[wins[i][0]] == board[wins[i][1]] &&
board[wins[i][0]] == board[wins[i][2]])
return board[wins[i][2]];
}
return 0;
}
int minimax(int board[9], int player) {
//How is the position like for player (their turn) on board?
int winner = win(board);
if(winner != 0) return winner*player;
move = -1;
int score = -2;//Losing moves are preferred to no move
int i;
for(i = 0; i < 9; ++i) {//For all moves,
if(board[i] == 0) {//If legal,
board[i] = player;//Try the move
int thisScore = -minimax(board, player*-1);
if(thisScore > score) {
score = thisScore;
move = i;
}//Pick the one that's worst for the opponent
board[i] = 0;//Reset board after try
}
}
if(move == -1) return 0;
return score;
}
void computerMove(int board[9]) {
int move = -1;
int score = -2;
int i;
for(i = 0; i < 9; ++i) {
if(board[i] == 0) {
board[i] = 1;
int tempScore = -minimax(board, -1);
board[i] = 0;
if(tempScore > score) {
score = tempScore;
move = i;
}
}
}
//returns a score based on minimax tree at a given node.
board[move] = 1;
}
void playerMove(int board[9]) {
int move = 0;
do {
printf("\nInput move ([0..8]): ");
scanf("%d", &move);
printf("\n");
} while (move >= 9 || move < 0 && board[move] == 0);
board[move] = -1;
}
int main() {
int board[9] = {0,0,0,0,0,0,0,0,0};
//computer squares are 1, player squares are -1.
printf("Computer: O, You: X\nPlay (1)st or (2)nd? ");
int player=0;
scanf("%d",&player);
printf("\n");
unsigned turn;
for(turn = 0; turn < 9 && win(board) == 0; ++turn) {
if((turn+player) % 2 == 0)
computerMove(board);
else {
draw(board);
playerMove(board);
}
}
switch(win(board)) {
case 0:
printf("A draw. How droll.\n");
break;
case 1:
draw(board);
printf("You lose.\n");
break;
case -1:
printf("You win. Inconceivable!\n");
break;
}
}
Here is the essence of minimax:
int minimax(int board[9], int player) {
// ....
for(i = 0; i < 9; ++i) { //For all moves,
// ....
int thisScore = -minimax(board, player*-1);
}
}
Go through each possible move, and for each such possible move, turn the board around, pretend to be the other player (that's the player*-1 part), and try each possible move. thisScore is set to the negative return value from the recursive call to minimax, since good for the other player equals bad for ourselves.
computerMove just goes through all the possible moves, calls minimax for each such possible move, and uses the one with the best result.

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