• Welcome to League Of Reason Forums! Please read the rules before posting.
    If you are willing and able please consider making a donation to help with site overheads.
    Donations can be made via here

My tic-tac-toe display/core

arg-fallbackName="CosmicJoghurt"/>
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.
 
arg-fallbackName="Master_Ghost_Knight"/>
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.
Have you tryed the lower case "true"?
 
arg-fallbackName="CosmicJoghurt"/>
Works!

You, sir, have earned yourself one cookie.

And a big thank you!

I'll work on two player games in a single PC, then on network programming.

Any good guides on network programming?
 
arg-fallbackName="Master_Ghost_Knight"/>
CosmicJoghurt said:
Any good guides on network programming?
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.
One advice that works for every system is, learn to multi-thread first (as it will be usefull to manage connections without the program getting stuck until it recieves something from the other end), how to multi-thread? It changes from OS to OS. You will have to google it and it won't be easy to find, specially for MAC OS.
 
arg-fallbackName="Snufkin"/>
CosmicJoghurt said:
Weird. The same code, compiled on Xcode 3 works. In Xcode 4 it doesn't work well. Bugged.
It's possibly something sneaky like an uninitialized variable.

If you aren't familiar with the debugger, get stuck in - the time spent learning how to use a debugger properly easily pays off.
 
arg-fallbackName="CosmicJoghurt"/>
Snufkin said:
CosmicJoghurt said:
Weird. The same code, compiled on Xcode 3 works. In Xcode 4 it doesn't work well. Bugged.
It's possibly something sneaky like an uninitialized variable.

If you aren't familiar with the debugger, get stuck in - the time spent learning how to use a debugger properly easily pays off.

No idea, what you mean, uninitialized variable...
 
arg-fallbackName="CosmicJoghurt"/>
main.cpp
Code:
#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
Code:
/*
 *  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;
        }
    }
}



There is a LOT of work to be done, but at least it works, 100%.


I know you've been DYING for a working version of my new game. Better than Skyrim!
 
arg-fallbackName="Master_Ghost_Knight"/>
I have seen this a while back, but it was late and I decided to reply the next morning, which I forgot.
But better late than never, here are 2 suggestions that I would make:
1. On main.cpp where you find :
Code:
 	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';
}
It should be this:
Code:
 	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';
}
And why is that?
You basically have something like this:
condition "A":
Action "B"
else:
Action "C"
condition "A":
Action "D"
else:
Action "E"
So unless the action "B" or "C" changes the condition "A" (or A does a tangent operation that does something else, which isn't the case), then if "A" is true after action B is performed then action D will always follow as well. Similarly if "A" is false it will execute C first then it will always execute "E".
So if you write it like this:
condition "A":
Action "B"
Action "D"
else:
Action "C"
Action "E"
It will perform the actions just the same, except that now you don't need to repeat the operation of checking the A.

2. On hearder.h similarly, where it reads:
Code:
 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; 
	}
}
It should be:
Code:
 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; 
	}
}
Because printing the first line will always happen before everything else, and more than that it will only happen once, so you don't need to force a check on this parameter for every "for" cycle.
I don't know if this helps, but you can think in terms of states, dependencies and the minimal amount of operations that you need to perform.
If one operation is independent of the one being executed before, then you can switch the order of the 2. If your operation need only to be performed once, only once and always once then you don't need to check if you want to perform that operation or not, you just do it once and don't put it in a cycle. You may need to check for a certain condition A to select either to perform certain operations or not, if you code smartly I would say that 90% of the cases you only have to check for that condition once in your entire code, if you do it more than once while your condition is not expected to change then you are probably doing something inefficient. Forget the temporal order by witch you would mentally perform the operations, do it instead in the order that will minimize the number of steps required to complete the task, and that may mean changing the order of things around (as long as they are interchangeable ofcourse). But I fear that sensitivity for this sort of things may only come from practice.
Despite that, it is a good progress.
 
Back
Top