0

Hey all.
I've been recently trying to write down a program that lets 2 players play tic-tac-toe (aka X/O) but I wanna code it using Bloodshed C++. Thing is, I wanna do it something like this.

---- > Display the gameboard like this.

1 2 3
4 5 6
7 8 9

---- > I ask the first player to enter the number which he wishes to replace with "X".

---- > In the program, I replace the number on the scoreboard with an "X"

---- > Display updated board, ask player 2 for where he wants to place a "O"

---- > Repeat the same thing of replacement, and update.

Thing is, I tried to like code the first round of the program. My code was fine (no syntax error, no logic error). But, I have logic errors in my program whereby the desired output isnt returned to players 1 and 2.

#include <iostream>
#include <string>
using namespace std;
int main ()
{
 string a = "1"; 
 string b = "2";
 string c = "3";
 string d = "4";
 string e = "5";
 string f = "6";
 string g = "7";
 string h = "8"; 
 string i = "9";
 int a1=1;
 int a2=2;
 int a3=3;
 int a4=4;
 int a5=5;
 int a6=6;
 int a7=7;
 int a8=8;
 int a9=9;
 int input1;
 int input2;
 string x ="X";
 string o = "O";
 cout << "Welcome to tic-tac-toe!"<< endl;
 cout << "This is a two player game" << endl;
 cout << endl;
 cout << "Player 1 will have X and player 2 will have O"<< endl;
 cout << "The game board by default is as follows"<< endl;
 cout << endl;
 cout << a << " " << b << " " << c << endl;
 cout << d << " " << e << " " << f << endl;
 cout << g << " " << h << " " << i << endl;
 cout << "Player one, enter the number which you want to replace with X" << endl;
 cin >> input1; 
 
 if (input1 = a1)
 a = x;
 else if (input1 =a2)
 b = x;
 else if (input1 =a3)
 c = x;
 else if (input1 =a4)
 d = x;
 else if (input1 =a5)
 e = x;
 else if (input1 =a6)
 f = x;
 else if (input1 =a7)
 g = x;
 else if (input1 =a8)
 h = x;
 else if (input1 =a9)
 i = x;

 cout << "The board has been updated" << endl;
 cout << a << " " << b << " " << c << endl;
 cout << d << " " << e << " " << f << endl;
 cout << g << " " << h << " " << i << endl;
 cout << "Player two, enter the number which you want to replace with O" << endl;
 cin >> input2;
 
 if (input2 = a1)
 a = o;
 else if (input2 =a2)
 b = o;
 else if (input2 =a3)
 c = o;
 else if (input2 =a4)
 d = o;
 else if (input2 =a5)
 e = o;
 else if (input2 =a6)
 f = o;
 else if (input2 =a7)
 g = o;
 else if (input2 =a8)
 h = o;
 else if (input2 =a9)
 i = o;
 
 cout << "The board has been updated" << endl;
 cout << a << " " << b << " " << c << endl;
 cout << d << " " << e << " " << f << endl;
 cout << g << " " << h << " " << i << endl;
 
 system ("pause");
 return 0;
}

Where's the mistake?!

I can tell the things going wrong in terms of logic errors if you all want me to....

Thanks in advance for reading.

3
Contributors
5
Replies
6
Views
9 Years
Discussion Span
Last Post by John A
0

Ever heard of loops and arrays? And by the way, using = for comparison is wrong. You must use ==, or you'll end up with a condition that never changes.

0

Erm, I'm thinking of a loop, but I'm on the fence between for and while... any suggestions?!

0

>I'm on the fence between for and while... any suggestions?!
Think about how it would have to loop. Is this going to be running for a known number of times, or is it until a certain condition is true? Thinking about these things will surely give you a better idea of what kind of loop you need to use.

0

You must use ==, or you'll end up with a condition that never changes.

Oh it changes alright, when the expression or L value on the RHS becomes zero. Try it out...

0

>Oh it changes alright, when the expression or L value on the RHS becomes zero. Try it out...
OK, nevermind. I see what the OP was trying to do now...

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.