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 ifelse{player = 1;}//close of else}}//close of main functionvoid initializeBoard(char board[][3]){int i = 0;int j = 0;while (i < 4){while (j < 4){board[i][j] = ' ';j++;}//close of while loopj = 0;i++;}//close of while loop}//close of initializeBoardvoid 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 displayBoardvoid 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 ifelse{board[row][column] = 'O';}//close of else}//close of makeMove
Best Answer
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 checkschar 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;elsereturn -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 turncout<<"\nPlayer "<<player<< " ,Enter Your Choice: ";cin>>choice;sym = (player == 1)? 'X' : 'O'; //assigns X to player 1 and O to player 2if(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!!";elsecout<<"\n Game Draw";getch();}int main(){clrscr();input(); return 0;}