CosmicJoghurt
New Member
For some reason I had to replace TRUE/FALSE with 1/0. It told me TRUE wasn't declared in this scope. Weird, I've never had that error before.
Follow along with the video below to see how to install our site as a web app on your home screen.
Note: This feature may not be available in some browsers.
Have you tryed the lower case "true"?CosmicJoghurt said:For some reason I had to replace TRUE/FALSE with 1/0. It told me TRUE wasn't declared in this scope. Weird, I've never had that error before.
That is a subject that I might not be able to help, this is one of those things that is difrent from OS to OS. When I learn it was on linux, now I will be relearning for windows for an upcoming project and it is difrent than on linux, and for mac it will be difrent than what is for windows or linux.CosmicJoghurt said:Any good guides on network programming?
It's possibly something sneaky like an uninitialized variable.CosmicJoghurt said:Weird. The same code, compiled on Xcode 3 works. In Xcode 4 it doesn't work well. Bugged.
Snufkin said:It's possibly something sneaky like an uninitialized variable.CosmicJoghurt said:Weird. The same code, compiled on Xcode 3 works. In Xcode 4 it doesn't work well. Bugged.
If you aren't familiar with the debugger, get stuck in - the time spent learning how to use a debugger properly easily pays off.
#include <iostream>
#include <string>
#include "Board.h"
using namespace std;
/* Displaying the board works the following way:
There's a 3x3 array to hold the X/O's for the board.
First, the show function prints the initial line of the tic-tac-toe board.
This initial line is printed every time a horizontal set of 3 X/O's is done printing.
Then, the actual X/O printing starts at the 1x1 spot of the board/array[0][0].
It prints each X/O's first line, in
the first set of 3 horizontal X/O's of the board. Then it does the same for each of the two remaining sets of 3 X/O's
in the board. When it's done printing any of the lines for each set of 3 X/O's,
it prints the final character of each line, a '|'. It's part of the outline of the board.
*/
int main (int argc, char * const argv[]) {
string player1, player2;
char endChar;
board.pInstructions();
cout << "What's player 1's name?\n";
getline(cin,player1);
cin.ignore(0);
cout << "What's player 2's name?\n";
getline (cin,player2);
char XorO;
char pLine, pColumn;
bool gameOver;
char player = '1';
board.show();
while (gameOver == 0) {
pLine = 0;
pColumn = 0;
if (player == '1'){
cout << player1 << " - it's your turn to play!\n";
}
else {
cout << player2 << " - it's your turn to play!\n";
}
if (player == '1') {
cout << "You play the X!\n";
XorO = 'X';
}
else {
cout << "You play the O!\n";
XorO = 'O';
}
while (true) {
cout << "Choose a line from 1-3!\n";
cin >> pLine;
pLine=pLine-48;
if (pLine < 4 && pLine > 0) break;
}
while (true) {
cout << "Choose a column from 1-3!\n";
cin >> pColumn;
pColumn=pColumn-48;
if (pColumn < 4 && pColumn > 0) break;
}
if (board.set(pLine,pColumn,XorO)){
if (player == '1') player = '2';
else player = '1';
}
board.show();
switch (board.checkWin()) {
case 1:
for (int n2 = 0; n2<100; n2++){
cout << player2 << " wins!\n";
cout << player1 << " is a fucking idiot!\n";
}
cout << "Type R to play again or anything else to exit the game!\n";
cin >> endChar;
if (endChar == 'R' || endChar == 'r') {
board.clearBoard();
}
else {
gameOver = 1;
}
break;
case 2:
for (int n1 = 0; n1<100; n1++) {
cout << player1 << " wins!\n";
cout << player2 << " is a fucking idiot!\n";
}
cout << "Type R to play again or anything else to exit the game!\n";
cin >> endChar;
if (endChar == 'R' || endChar == 'r') {
board.clearBoard();
}
else {
gameOver = 1;
}
break;
}
}
return 0;
}
/*
* Board.h
* C++ Practice Program #1
*
* Created by CosmicJoghurt on 8/25/11.
* Copyright 2011 __MyCompanyName__. All rights reserved.
*
*/
#include <string>
#include <iostream>
using namespace std;
/* Creating the strings for circles, crosses, empty rooms, the final board column and
the lines in between of each line of three X/O spots.*/
string Circle1 = "|/ \\"; // For the printing of each X/O there are two lines:
string Circle2 = "|\\ /";
string Cross1 = "| \\/ ";
string Cross2 = "| /\\ ";
string Empty = "| ";
string Column = "|"; // When the X/O are done printing we need the last column of the board to be printed;
string Line = " ---- ---- ----";
class Board {
char spot[3][3];
int checkX, checkO;
int checkColumn, checkLine;
public:
bool set(int,int,char);
void show(void);
void pInstructions(void);
int checkWin(void);
void clearBoard(void);
}board;
bool Board::set(int line, int column, char x_o){
int fLine, fColumn;
fLine = --line;
fColumn = --column;
if (spot[fLine][fColumn] == 0){
if (x_o == 'X' || x_o == 'x') {
spot[fLine][fColumn] = 'X';
}
else {
spot[fLine][fColumn] = 'O';
}
return 1;
}
else {
cout << "Spot is already occupied! Choose another spot!\n";
return 0;
}
}
void Board::show(void){ /* Function to be called for showing the current board */
for (int line = 0; line<3; line++) // One iteration of the *for* loop for each line of X/O's.
{
if (line == 0){ // Only displays bLine once, since it displays at the end of each X/O line.
cout << Line << endl;// Line in between of each set of X/O's and at the beginning of each board.
}
for (int b = 0; b<2; b++){ // Each X/O has two lines for /'s and \'s.
for (int column = 0; column<3; column++) { // One iteration of the *for* loop for each X/O.
switch (spot[line][column]) {
case 'X':
if (b == 0)
cout << Cross1; // First line for the X.
else
cout << Cross2; // Second line for the X.
break;
case 'O':
if (b == 0)
cout << Circle1;
else
cout << Circle2;
break;
default:
cout << Empty;
break;
}
}
cout << Column << endl; // Print the last column of the board.
}
cout << Line << endl; // Line in between of each of the 3 lines of X/O's.
}
}
void Board::pInstructions(void){
cout << "Welcome to Tic-Tac-Toe!\nThis is the initial, empty board:\n";
show();
cout << "Each player will be asked to indicate which line and column they'll play.\n";
cout << "This is a line ---------------------->\n";
cout << "This is a column\n";
cout << " |\n";
cout << " |\n";
cout << " |\n";
cout << " |\n";
cout << " |\n";
cout << " |\n";
cout << " |\n";
cout << " |\n";
cout << " |\n";
cout << " |\n";
cout << " V\n";
}
int Board::checkWin(void){
for (checkLine = 0; checkLine<3; checkLine++){
for (checkColumn = 0; checkColumn<3; checkColumn++){
if (spot[checkLine][checkColumn] == 'X'){
++checkX;
}
if (spot[checkLine][checkColumn] == 'O'){
++checkO;
}
}
if (checkX == 3) {
return 2;
}
if (checkO == 3) {
return 1;
}
checkX = 0;
checkO = 0;
}
for (checkColumn = 0; checkColumn<3; checkColumn++){
for (checkLine = 0; checkLine<3; checkLine++){
if (spot[checkLine][checkColumn] == 'X'){
++checkX;
}
if (spot[checkLine][checkColumn] == 'O'){
++checkO;
}
}
if (checkX == 3) {
return 2;
}
if (checkO == 3) {
return 1;
}
checkX = 0;
checkO = 0;
}
if (spot[0][0] == 'X' && spot[1][1] == 'X' && spot[2][2] == 'X'){
return 2;
}
else if (spot[0][0] == 'O' && spot[1][1] == 'O' && spot[2][2] == 'O'){
return 1;
}
if (spot[2][0] == 'X' && spot[1][1] == 'X' && spot[0][2] == 'X'){
return 2;
}
else if (spot[2][0] == 'O' && spot[1][1] == 'O' && spot[0][2] == 'O'){
return 1;
}
return 0;
}
void Board::clearBoard(void){
for (int n = 0; n<3; n++) {
for (int v = 0; v<3; v++) {
spot[n][v] = 0;
}
}
}
if (player == '1'){
cout << player1 << " - it's your turn to play!\n";
}
else {
cout << player2 << " - it's your turn to play!\n";
}
if (player == '1') {
cout << "You play the X!\n";
XorO = 'X';
}
else {
cout << "You play the O!\n";
XorO = 'O';
}
if (player == '1'){
cout << player1 << " - it's your turn to play!\n";
cout << "You play the X!\n";
XorO = 'X';
}
else {
cout << player2 << " - it's your turn to play!\n";
cout << "You play the O!\n";
XorO = 'O';
}
void Board::show(void){
for (int line = 0; line<3; line++) {
if (line == 0){
cout << Line << endl;
}
for (int b = 0; b<2; b++){ // Each X/O has two lines for /'s and \'s.
for (int column = 0; column<3; column++) { switch (spot[line][column]) {
case 'X':
if (b == 0)
cout << Cross1; // First line for the X.
else
cout << Cross2; // Second line for the X.
break;
case 'O':
if (b == 0)
cout << Circle1;
else
cout << Circle2;
break;
default:
cout << Empty;
break;
}
}
cout << Column << endl;
}
cout << Line << endl;
}
}
void Board::show(void){
[color=red]cout << Line << endl;[/color]
for (int line = 0; line<3; line++) {
for (int b = 0; b<2; b++){
for (int column = 0; column<3; column++) { switch (spot[line][column]) {
case 'X':
if (b == 0)
cout << Cross1; // First line for the X.
else
cout << Cross2; // Second line for the X.
break;
case 'O':
if (b == 0)
cout << Circle1;
else
cout << Circle2;
break;
default:
cout << Empty;
break;
}
}
cout << Column << endl;
}
cout << Line << endl;
}
}