I have written a program that allows for two people to play tictactoe, however it will not work. I am not sure what the problem is. Can some please help? Thank you in advance
bpress91 0 Newbie Poster
/*
* tictactoe.cpp
*
*
* Created by Press, Briana on 10/24/10.
* Copyright 2010 __MyCompanyName__. All rights reserved.
*
*/
// This program will implement the two person game tic-tac-toe
#include <iostream>
#include <iomanip>
#include <cstring>
using namespace std;
// Function prototypes
void SetBoard (char TheBoard[][3], int choice1);
void SetBoard ( int position, char mark);
void DisplayBoard();
void Winner();
char TheBoard[3][3];
int main()
{
char name[10], name2[10], choice1;
int choice, count;
// Prompts each player for full name
cout << " Player one : input name " << endl;
cin.getline(name,20);
cout << " Player two : input name " << endl;
cin.getline(name2,20);
SetBoard( TheBoard,choice1);
DisplayBoard();
//do
{
cout << " Make a choice " << name << endl;
cin >> choice;
while (choice < 1 || choice > 9)
cout << " Bad Choice. Try again (1-9) " << endl;
DisplayBoard();
// while(
DisplayBoard();
return 0;
}
void SetBoard() // Sets the board so that it represents the nine positions
{
TheBoard[0][0] = '1' ;
TheBoard[0][1] = '2' ;
TheBoard[0][2] = '3' ;
TheBoard[1][0] = '4' ;
TheBoard[1][1] = '5' ;
TheBoard[1][2] = '6' ;
TheBoard[2][0] = '7' ;
TheBoard[2][1] = '8' ;
TheBoard[2][2] = '9' ;
}
void DisplayBoard() // Makes the board
{
cout << TheBoard[0][0] << " | " << TheBoard[0][1] << " | " << TheBoard[0][2] << endl;
cout << " _ + _ + _ " << endl;
cout << TheBoard[1][0] << " | " << TheBoard[1][1] << " | " << TheBoard[1][2] << endl;
cout << " _ + _ + _ " << endl;
cout << TheBoard[2][0] << " | " << TheBoard[2][1] << " | " << TheBoard[2][2] << endl;
}
void SetBoard(int position, char mark)
{
switch(position)
{
case '1':
if(TheBoard[0][0] == 'X' || TheBoard[0][0] == 'O')
cout << " Mark ";
break;
case '2' :
if(TheBoard[0][1] == 'X' || TheBoard[0][1] == 'O')
cout << " Mark ";
break;
case '3':
if(TheBoard[0][2] == 'X' || TheBoard[0][2] == 'O')
cout << "Mark ";
break;
case '4':
if(TheBoard[1][0] == 'X' || TheBoard[1][0] == 'O')
cout << "Mark ";
break;
case '5':
if(TheBoard[1][1] == 'X' || TheBoard[1][1] == 'O')
cout << "Mark ";
break;
case '6':
if(TheBoard[1][2] == 'X' || TheBoard[1][2] == 'O')
cout << "Mark ";
break;
case '7':
if(TheBoard[2][0] == 'X' || TheBoard[2][0] == 'O')
cout << "Mark ";
break;
case '8':
if (TheBoard[2][1] == 'X' || TheBoard[2][1] == 'O')
cout << "Mark ";
break;
case '9':
if (TheBoard[2][2] == 'X' || TheBoard[2][2] == 'O')
cout << "Mark ";
break;
default:
else
cout << "Please choose a number between 1 and 9 that is not already taken ";
}
switch (choice)
{
case '1':
if(choice == 1)
TheBoard[0][0] = 'X';
break;
case '2':
if(choice == 2)
TheBoard[0][1] = 'X';
break;
case '3' :
if (choice == 3)
TheBoard[0][2] = 'X';
break;
case '4' :
if(choice == 4)
TheBoard[1][0] = 'X';
break;
case '5' :
if(choice == 5)
TheBoard[1][1] = 'X';
break;
case '6' :
if(choice == 6)
TheBoard[1][2] = 'X';
break;
case '7' :
if (choice == 7)
TheBoard[2][0] = 'X';
break;
case '8' :
if (choice == 8)
TheBoard[2][1] = 'X';
break;
case '9' :
if (choice == 9)
TheBoard[2][2] = 'X';
break;
default:
cout << " Please choose 1-9, Thank You " << endl;
break;
}
DisplayBoard();
cout << " Make a choice " << name2 << endl;
cin >> choice;
DisplayBoard();
switch (choice)
{
case '1':
if(choice == 1)
TheBoard[0][0] = 'O';
break;
case '2':
if(choice == 2)
TheBoard[0][1] = 'O';
break;
case '3' :
if (choice == 3)
TheBoard[0][2] = 'O';
break;
case '4' :
if(choice == 4)
TheBoard[1][0] = 'O';
break;
case '5' :
if(choice == 5)
TheBoard[1][1] = 'O';
break;
case '6' :
if(choice == 6)
TheBoard[1][2] = 'O';
break;
case '7' :
if (choice == 7)
TheBoard[2][0] = 'O';
break;
case '8' :
if (choice == 8)
TheBoard[2][1] = 'O';
break;
case '9' :
if (choice == 9)
TheBoard[2][2] = 'O';
break;
default:
cout << " Please choose 1-9, Thank You " << endl;
break;
}
}
void Winner()
{
{
if( TheBoard[0][0] == 'X' && TheBoard[0][1] == 'X' && TheBoard[0][2] == 'X')
cout << " Winner!!";
else if( TheBoard[1][0] == 'X' && TheBoard[1][1] == 'X' && TheBoard[1][2] == 'X')
cout << " Winner !!";
else if( TheBoard[2][0] == 'X' && TheBoard[2][1] == 'X' && TheBoard[2][2] == 'X')
cout << " Winner !!";
else if( TheBoard[0][0] == 'X' && TheBoard[1][0] == 'X' && TheBoard[2][0] == 'X' )
cout << " Winner !!";
else if(TheBoard[0][1] == 'X' && TheBoard[1][1] == 'X' && TheBoard[2][1] == 'X' )
cout << " Winner !!";
else if(TheBoard[0][2] == 'X' && TheBoard[1][2] == 'X' && TheBoard[2][2] == 'X' )
cout << " Winner !!";
else if(TheBoard[0][0] == 'X' && TheBoard[1][1] == 'X' && TheBoard[2][2] == 'X')
cout << " Winner !!";
else if(TheBoard[0][2] == 'X' && TheBoard[1][1] == 'X' && TheBoard[2][0] == 'X')
cout << " Winner !!";
else
cout << "NO WINNER :/ " << endl;
}
{
if( TheBoard[0][0] == 'O' && TheBoard[0][1] == 'O' && TheBoard[0][2] == 'O')
cout << " Winner !!";
else if( TheBoard[1][0] == 'O' && TheBoard[1][1] == 'O' && TheBoard[1][2] == 'O')
cout << " Winner !!";
else if( TheBoard[2][0] == 'O' && TheBoard[2][1] == 'O' && TheBoard[2][2] == 'O')
cout << " Winner !!";
else if( TheBoard[0][0] == 'O' && TheBoard[1][0] == 'O' && TheBoard[2][0] == 'O' )
cout << " Winner !!";
else if(TheBoard[0][1] == 'O' && TheBoard[1][1] == 'O' && TheBoard[2][1] == 'O' )
cout << " Winner !!";
else if(TheBoard[0][2] == 'O' && TheBoard[1][2] == 'O' && TheBoard[2][2] == 'O' )
cout << " Winner !!";
else if(TheBoard[0][0] == 'O' && TheBoard[1][1] == 'O' && TheBoard[2][2] == 'O')
cout << " Winner !!";
else if(TheBoard[0][2] == 'O' && TheBoard[1][1] == 'O' && TheBoard[2][0] == 'O')
cout << " Winner !!";
else
cout << "NO WINNER :/ " << endl;
}
}
}
WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague
Neither are we. You didn't describe the problem. And most people will not download attachments.
technologic 0 Newbie Poster
I have downloaded the attachment, and tried to compile it, but i found many mistakes. I think everything's alright, try to check it for mistakes. Good Luck!
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.