Beginner Programming Help

Hi there, I am new to the forums and new to the world of C++. I understand the basics and what everything does but like most newbies out there I am still trying to work it all together properly. I have started by making small text style games in the console and I tried to make tic-tac-toe.So far the code works fine for what I have added but there are a few things that don't seem to function properly.
Number 1# When the location function (either of the 2) runs it seems to work fine and then asks for a location on the board, when you put it in it it is supposed to run the code again if it is not a number 1-9 or if there is already another number in there. I am still trying to figure this out. Number 2# In the gameRegulations function it seems to work fine so far but for some reason it breaks the code if I add anything else to it such as re-printing the board or adding more instances to the if parameters.------------------------------------------------------------------

So these are the main two problems I am experiencing. But besides that I know someone will bring up using multi-dimensional arrays and I know that I should but I want to get a grasp of regular arrays before I do more complex multi-Dimensional.-----------Also I would like to know how I can optimize my code more and make it more effecient. Any help would be appreciated also I have stopped myself from looking at other tic-tac-toe posts because if I do I am more likely to just copy the code and not understand it than if I would figure it out on my own (atleast for the time being), so with that being said If you would be kind(or mean) enough to give me hints instead of answers then I would probably learn and understand what I will write much more. Thanks for your time guys :D
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
// HangMan.cpp : Defines the entry point for the console application.
#include "stdafx.h"
#include <iostream>

void location1(int *location, char table[]);
void location2(int *location, char table[]);

void drawTable (char table[]);
void gameRegulations(char table[], bool turnCounter);

	
	int main(int)
{

	int locationHolder = 0;
	
	char gameBoard[9] =		{'1','2','3',
							 '4','5','6',
							 '7','8','9'};

	drawTable (gameBoard);
	location1(&locationHolder, gameBoard);

	
	return 0;
}

void location1(int *location, char table[]){
	

	int number;
	bool turnCounter;
	
	if(turnCounter != 1){
		char enemy = 'X';
		do 
		{
		std::cout << "Please choose an unused square from the grid 1-9" << std::endl;
		std::cin >> number;
		}
		while((number < 1 || number > 9) & (table[number-1] != 'X'));
		
		table[number-1]='O';
		drawTable(table);
		std::cout << "Player 1 Moved......Player 2's Turn" << std::endl;
		gameRegulations(table, turnCounter);
		location2(location, table);
	}

	



}



void location2(int *location, char table[]){
	

	int number;
	bool turnCounter;
	
	if(turnCounter != 0){
		char enemy = 'O';
		do 
		{
		std::cout << "Please choose an unused square from the grid 1-9" << std::endl;
		std::cin >> number;
		}
		while((number < 1 || number > 9) & (table[number-1] != 'O'));

		table[number-1]='X';

		std::cout << "Player 2 Moved......Player 1's Turn" << std::endl;
		*location=number-1;
		drawTable(table);
		gameRegulations(table, turnCounter);
		location1(location, table);
	}
	



}


void drawTable(char table[])
{
		std::cout << "=======" << std::endl;
		std::cout << "|" << table[0] << "|" << table[1] << "|" << table[2] << "|" << std::endl;
		std::cout << "|" << "=====" << "|" << std::endl;
		std::cout << "|" << table[3] << "|" << table[4] << "|" << table[5] << "|" << std::endl;
		std::cout << "|" << "=====" << "|" << std::endl;
		std::cout << "|" << table[6] << "|" << table[7] << "|" << table[8] << "|" << std::endl;
		std::cout << "=======" << std::endl;
}

void gameRegulations(char table[], bool turnCounter){
	
	if((table[0]=='O')&(table[1]=='O')&(table[2]=='O')){
		std::cout << "Player " << turnCounter+1 << " Has Won the Game with Horizontal Win" << std::endl;
		for(int i = 0;i<9;i++){
			table[i]='*';
		}

	

	}

}


Last edited on
Can anyone help?
@curscascis

Okay. I added a bit to your program. I added a do/while loop so that the request for an input doesn't stop after the first one. It checks if the space on the board is already used, by either player, and also checks now if either player has a win. But, the game will continue on even AFTER the win, since i did not add a end game scenario. You can do that.
Good start on the game..

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
// Tic-Tac-Toe.cpp : main project file.
#include "stdafx.h"
#include <iostream>

void location1(int *location, char table[]);
void location2(int *location, char table[]);

void drawTable (char table[]);
void gameRegulations(char table[], bool turnCounter);


int main()
{
	int locationHolder = 0;

	char gameBoard[9] = {
		'1','2','3'
		,'4','5','6'
		,'7','8','9'
	};

	do
	{
		drawTable (gameBoard);
		location1(&locationHolder, gameBoard);
	} while (locationHolder !=1);

	return 0;
}

void location1(int *location, char table[])
{
	int number;
	bool turnCounter = 0;
	do 
	{
		std::cout << "Please choose an unused square from the grid 1-9" << std::endl;
		std::cin >> number;
		if (table[number-1] == 'X' || table[number-1] == 'O')
			cout << "Choose again. That space already occupied.." << endl;
	} while((number < 1 || number > 9) || (table[number-1] == 'X' || table[number-1] == 'O'));

	table[number-1] = 'O';
	drawTable(table);
	std::cout << "Player 1 Moved...... Player 2's Turn" << std::endl;
	gameRegulations(table, turnCounter);
	location2(location, table);
}

void location2(int *location, char table[])
{
	int number;
	bool turnCounter = 1;

	do 
	{
		std::cout << "Please choose an unused square from the grid 1-9" << std::endl;
		std::cin >> number;
		if (table[number-1] == 'X' || table[number-1] == 'O')
			cout << "Choose again. That space already occupied.." << endl;
	} while((number < 1 || number > 9) || (table[number-1] == 'X' || table[number-1] == 'O'));

	table[number-1] = 'X';

	std::cout << "Player 2 Moved...... Player 1's Turn" << std::endl;
	*location=number-1;
	drawTable(table);
	gameRegulations(table, turnCounter);
	location1(location, table);
}


void drawTable(char table[])
{
	std::cout << "=======" << std::endl;
	std::cout << "|" << table[0] << "|" << table[1] << "|" << table[2] << "|" << std::endl;
	std::cout << "|" << "=====" << "|" << std::endl;
	std::cout << "|" << table[3] << "|" << table[4] << "|" << table[5] << "|" << std::endl;
	std::cout << "|" << "=====" << "|" << std::endl;
	std::cout << "|" << table[6] << "|" << table[7] << "|" << table[8] << "|" << std::endl;
	std::cout << "=======" << std::endl;
}

void gameRegulations(char table[], bool turnCounter)
{
	turnCounter++;
	if((table[0]=='O' && table[1]=='O' && table[2]=='O') || (table[3]=='O' && table[4]=='O' && table[5]=='O') || (table[6]=='O' && table[7]=='O' && table[8]=='O')
		||(table[0]=='X' && table[1]=='X' && table[2]=='X') || (table[3]=='X' && table[4]=='X' && table[5]=='X') || (table[6]=='X' && table[7]=='X' && table[8]=='X'))
	{
		std::cout << "Player " << turnCounter << " Has Won the Game with Horizontal Win" << std::endl;
		for(int i = 0;i<9;i++)
		{
			table[i]='*';
		}
	}
	if((table[0]=='O' && table[3]=='O' && table[6]=='0') || (table[1]=='O' && table[4]=='O' && table[7]=='O') || (table[2]=='O' && table[5]=='O' && table[8]=='O')
		|| (table[0]=='X' && table[3]=='X' && table[6]=='X') || (table[1]=='X' && table[4]=='X' && table[7]=='X') || (table[2]=='X' && table[5]=='X' && table[8]=='X'))
	{
		std::cout << "Player #" << turnCounter << " Has Won the Game with Vertical Win" << std::endl;
		for(int i = 0;i<9;i++)
		{
			table[i]='*';
		}
	}
	if((table[0]=='O' && table[4]=='O' && table[8]=='O') || (table[2]=='O' && table[4]=='O' && table[6]=='O')
		|| (table[0]=='X' && table[4]=='X' && table[8]=='X') || (table[2]=='X' && table[4]=='X' && table[6]=='X'))
	{
		std::cout << "Player #" << turnCounter << " Has Won the Game with Diagonal Win" << std::endl;
		for(int i = 0;i<9;i++)
		{
			table[i]='*';
		}
	}
}
Wow, thanks a lot I see what I did wrong thanks to your fixes. I am guessing one of them was using & instead of || in the check to see if space is used, and the other one I am not completely sure of but I will figure it out. Also on another note how can I use pointers to optimize this? I tried using one for location but I am not sure if i used it right. Thanks
Hey, so I got most of it working and put it together, I added some things and tried adding an ending but whenever I attempt to change the turnCounter using (what i think) is pass by reference I get (angMan.cpp(98): error C2664: 'gameRegulations' : cannot convert parameter 2 from 'int *' to 'int')

Not sure what this means :3 .. Anywho

here is what I got so far.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
// HangMan.cpp : Defines the entry point for the console application.
#include "stdafx.h"
#include <iostream>

void location1(int *location, char table[]);
void location2(int *location, char table[]);

void drawTable (char table[]);
void gameRegulations(char table[], int turnCounter);

	
	int main(int)
{
	int continueGame = 1;
	int locationHolder = 0;
	
	char gameBoard[9] =		{'1','2','3',
							 '4','5','6',
							 '7','8','9'};

	while(continueGame==1){
	std::cout << "=========================================" << std::endl;
	std::cout << "=======Welcome to Tic Tac Toe============" << std::endl;
	std::cout << "=========================================" << std::endl;
	std::cout << "\n" << std::endl;
	drawTable (gameBoard);
	location1(&locationHolder, gameBoard);
	std::cout << "Thanks for playing play again? 1- yes.....2-No" << std::endl;
	std::cin >> continueGame;
	}
		
	return 0;
}

void location1(int *location, char table[]){
	

	int number;
	int turnCounter=0;
	
	if(turnCounter == 0){
		char enemy = 'X';
		do 
		{
		std::cout << "Please choose an unused square from the grid 1-9" << std::endl;
		std::cin >> number;

			if(table[number-1]=='O'|| table[number-1]=='X'){
				std::cout << "I am sorry but that square is already in use," << std::endl;
			}
		}
		while((number < 1 || number > 9) || (table[number-1] == 'X')|| (table[number-1] == 'O'));
		
		table[number-1]='O';
		drawTable(table);
		std::cout << "Player 1 Moved......Player 2's Turn" << std::endl;
		gameRegulations(table, turnCounter);
		if(turnCounter == 0){
			location2(location, table);
		}



	}


	



}



void location2(int *location, char table[]){
	

	int number;
	int turnCounter=1;
	
	if(turnCounter == 1){
		char enemy = 'O';
		do 
		{
		std::cout << "Please choose an unused square from the grid 1-9" << std::endl;
		std::cin >> number;
		if(table[number-1]=='O'|| table[number-1]=='X'){
				std::cout << "I am sorry but that square is already in use," << std::endl;
			}
		}
		while((number < 1 || number > 9) || (table[number-1] == 'X')|| (table[number-1]== 'O'));

		table[number-1]='X';

		std::cout << "Player 2 Moved......Player 1's Turn" << std::endl;
		*location=number-1;
		drawTable(table);
		gameRegulations(table, turnCounter);
		if(turnCounter == 1){
			location1(location, table);
		}
	}
	



}


void drawTable(char table[])
{
		std::cout << "=======" << std::endl;
		std::cout << "|" << table[0] << "|" << table[1] << "|" << table[2] << "|" << std::endl;
		std::cout << "|" << "=====" << "|" << std::endl;
		std::cout << "|" << table[3] << "|" << table[4] << "|" << table[5] << "|" << std::endl;
		std::cout << "|" << "=====" << "|" << std::endl;
		std::cout << "|" << table[6] << "|" << table[7] << "|" << table[8] << "|" << std::endl;
		std::cout << "=======" << std::endl;
}

void gameRegulations(char table[], int *turnCounter){
	
	if((table[0]=='O' && table[1]=='O' && table[2]=='O') || (table[3]=='O' && table[4]=='O' && table[5]=='O') || (table[6]=='O' && table[7]=='O' && table[8]=='O')
		||(table[0]=='X' && table[1]=='X' && table[2]=='X') || (table[3]=='X' && table[4]=='X' && table[5]=='X') || (table[6]=='X' && table[7]=='X' && table[8]=='X'))
	{
		std::cout << "Player " << turnCounter+1 << " Has Won the Game with Horizontal Win" << std::endl;
		for(int i = 0;i<9;i++)
		{
			table[i]='*';
		}
		*turnCounter=2;

	}
	if((table[0]=='O' && table[3]=='O' && table[6]=='0') || (table[1]=='O' && table[4]=='O' && table[7]=='O') || (table[2]=='O' && table[5]=='O' && table[8]=='O')
		|| (table[0]=='X' && table[3]=='X' && table[6]=='X') || (table[1]=='X' && table[4]=='X' && table[7]=='X') || (table[2]=='X' && table[5]=='X' && table[8]=='X'))
	{
		std::cout << "Player #" << turnCounter+1 << " Has Won the Game with Vertical Win" << std::endl;
		for(int i = 0;i<9;i++)
		{
			table[i]='*';
		}
		*turnCounter=2;
	}
	if((table[0]=='O' && table[4]=='O' && table[8]=='O') || (table[2]=='O' && table[4]=='O' && table[6]=='O')
		|| (table[0]=='X' && table[4]=='X' && table[8]=='X') || (table[2]=='X' && table[4]=='X' && table[6]=='X'))
	{
		std::cout << "Player #" << turnCounter+1 << " Has Won the Game with Diagonal Win" << std::endl;
		for(int i = 0;i<9;i++)
		{
			table[i]='*';
		}
	*turnCounter=2;
	}


}
Topic archived. No new replies allowed.