Simple pirate ship game problem

Hey guys, i have my project here that im working on and i have hit a roadblock. I have been trying to solve it for the past couple of hours but maybe you guys can help me with some insight faster.

I'm making a game where you are a ship and are playing against the computer ship. Currently i am working on making each ship a structure so i can easily manipulate different aspects of the ship as i please. I am on the first part of my structure which is defining the agility (or how much moves it can make each turn) of the ship.. I want the agility to be asked from the user and depending on what he enters, that translates to how much times his ship can move in a turn.

I will show you guys what i have for my code:

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
#include <cstdlib>
#include <iostream>
#include <iomanip>
#include <ctime>


using namespace std;

    struct Ships
    {
           int p; //the position of the ship
           int agility; //the amount of moves it has each turn
    };

void gameStart (int, Ships, int, int[][10]);
void printBoard (int[][10], Ships&, int&);
void movement (int [][10], Ships&, int& );
void movementAttribute (Ships & p1);

int main(int argc, char *argv[])
{
    char choice;
    Ships p1;
    int computer;
    int gameboard[10][10];
    
    p1.p;
    p1.agility = 0;
    
    
    cout << "\n\n";
    cout << "\t Heyo, wanna play the game? (Y = yes, N = exit): ";
    cin >> choice;
    cout << "\n\n";
    
    gameStart(choice, p1, computer,gameboard);
    
    
         
    system("PAUSE");
    return EXIT_SUCCESS;
}

void gameStart (int choice, Ships p1, int computer, int gameboard[][10])
{
    switch (choice)
    {
        case 'Y':
        case 'y':
            srand(time(0));
            p1.p = rand()%99+1;
            computer = rand()%99+1;
            movementAttribute (p1);
            printBoard(gameboard, p1, computer);
            movement (gameboard, p1, computer);
            break;
            
        case 'N':
        case 'n':
            exit(0);
            break;
         
        default:
            cout << "Wrong input. It's either: Y to play, or N to quit.\n";
    }
}

void printBoard (int [] [10], Ships & p1, int & computer )
{
    for (int rows = 0; rows < 10; rows++)
    {
        for (int collumns = 0; collumns < 10; collumns++)
        {
			if ((p1.p / 10) == rows && (p1.p % 10) == collumns)
			{
				cout << " P";
			}
			else if ((computer / 10) == rows && (computer % 10) == collumns)
			{
				cout << " C";
			}
			//else if statement here to check computer
			else 
			{
				cout << " *";
			}
        }
        cout << endl; 
    }
}


void movement (int gameboard [][10], Ships & p1, int & computer)
{
    char move;
    
    
        do
        {
            for(int turns = 0; turns < p1.agility; turns++)
            {
                cout << "Please enter your moves : ";
                cin >> move;
                
                if (move == 'w')
                {
                    p1.p -= 10;
                    system ("CLS");
                    if (p1.p <= 0)
                    {
                        p1.p += 100;
                        system("CLS");
                    }
                }
                else if (move == 'a')
                {
                    p1.p -= 1;
                    system ("CLS");
                    if (p1.p == 9 || p1.p == 19|| p1.p == 29|| p1.p == 39|| p1.p == 49|| p1.p == 59|| p1.p == 69|| p1.p == 79|| p1.p == 89)
                    {
                        p1.p += 10;
                        system("CLS");
                    }
                }
                else if (move == 'd')
                {
                    p1.p += 1;
                    system ("CLS");
                    if (p1.p == 10 || p1.p == 20|| p1.p == 30|| p1.p == 40|| p1.p == 50|| p1.p == 60|| p1.p == 70|| p1.p == 80|| p1.p == 90)
                    {
                        p1.p -= 10;
                        system("CLS");
                    }
                }
                
            printBoard(gameboard, p1, computer);
        }
        
        }while (move == 'w' || move == 'a' || move == 'd');
    
}

void movementAttribute (Ships & p1)
{
    cout << "Please enter agility (only from 0 to 6): ";
    cin >> p1.agility;
    cout << endl;
    
}


Any help is appreciated, thanks!
hello friend, I am very happy to see your code as i am also a game developing enthusiast.
can you explain your problem again.
I want the agility to be asked from the user and depending on what he enters, that translates to how much times his ship can move in a turn.

isnt it just some cin >> call ?? or what is really ur problem....
The problem im having is that the game asks for the agility which is p1.agility in void movementAttribute (Ships & p1) but no matter what the user puts in (unless its 0), he can move an infinite number of times.

What i want is that when the user is asked to put in agility, he puts it in and then the agility controls the amount of moves the player can make in one turn. So lets say he put in 3 when asked for agility... he can only go up 3 times or left 3 times or up,left,up...etc.

I'm having trouble putting my problem into words, but i hope this clarified something. if not, keep asking me.
i think it can be a FPS (frames per second) problem.. because your computer is faster than your hand it can do a thing 100 times when you are just pressing a key.. so you have to control the rate which you are updating the screen.

i tried reading your code. but its hard to understand all of it, because i dont know your purpose of having each and every part of code. i think you should do more commenting. try to comment more. or you will also get lost after few days, believe me, it happens.

** i think it can be that cin gets what you enter as CHAR and its not actually value 3, but its 51 (ASCII value of 3 ), so you will have to substract 48 from it to get the number.
use the ascii table below... see that 0 is 48th character and you can get any single digit by substracting 48 from it.
http://www.cs.utk.edu/~pham/ascii_table.jpg

ps.
i heard some people say system() function is not a very good thing to use. i dont know why and i don know about that call very much. i know that it will only work in windows and will not work on linux. maybe you can start a thread asking for another way to do it which is portable.
I got it working . NOW.. how would i go about making turns for the cpu and the player? What i want is that once the player is done making his moves, the cpu would go making his move (the cpu is all randomized, it chooses its own agility randomly for the game (from 0-6) and makes random moves). How would i make the cpu do this? And the game take turns until they are dead (which i will work on later, but right now what i want is the computer to make randomized moves based on the random agility points it chose at the beginning of the game).

Here's the code:

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
157
158
159
160
161
162
163
#include <cstdlib>
#include <iostream>
#include <iomanip>
#include <ctime>


using namespace std;

    struct Ships
    {
           int p; //the position of the ship
           int agility; //the amount of moves it has each turn
    };

void gameStart (int, Ships, int, int[][10]);
void printBoard (int[][10], Ships&, int&);
void movement (int [][10], Ships&, int& );
void movementAttribute (Ships & p1);

int main(int argc, char *argv[])
{
    char choice;
    Ships p1;
    int cpu;
    int gameboard[10][10];
    
    p1.p;
    p1.agility;
    
    
    cout << "\n\n";
    cout << "\t Heyo, wanna play the game? (Y = yes, N = exit): ";
    cin >> choice;
    cout << "\n\n";
    
    gameStart(choice, p1, cpu,gameboard);
    
    
         
    system("PAUSE");
    return EXIT_SUCCESS;
}

void gameStart (int choice, Ships p1, int cpu, int gameboard[][10])
{
    switch (choice)
    {
        case 'Y':
        case 'y':
            srand(time(0));
            p1.p = rand()%99+1;
            cpu = rand()%99+1;
            printBoard(gameboard, p1, cpu);
            movementAttribute (p1);
            movement (gameboard, p1, cpu);
            break;
            
        case 'N':
        case 'n':
            exit(0);
            break;
         
        default:
            cout << "Wrong input. It's either: Y to play, or N to quit.\n";
    }
}

void printBoard (int [] [10], Ships & p1, int & cpu )
{
    for (int rows = 0; rows < 10; rows++)
    {
        for (int collumns = 0; collumns < 10; collumns++)
        {
			if ((p1.p / 10) == rows && (p1.p % 10) == collumns)
			{
				cout << " P";
			}
			else if ((cpu / 10) == rows && (cpu % 10) == collumns)
			{
				cout << " C";
			}
			//else if statement here to check cpu
			else 
			{
				cout << " *";
			}
        }
        cout << endl; 
    }
}


void movement (int gameboard [][10], Ships & p1, int & cpu)
{
    char move;
    
    
    
    for (int moves = 0; moves < p1.agility; moves++)
    {
        cout << "Please enter your moves : ";
        cin >> move;      
        switch (move)
        {
            case 'W':
            case 'w':
                    p1.p -= 10;
                    system ("CLS");
                    printBoard(gameboard, p1, cpu);
                    if (p1.p <= 0)
                    {
                        p1.p += 100;
                        system("CLS");
                        printBoard(gameboard, p1, cpu);
                    }
                    break;
            case 'A':
            case 'a': 
                p1.p -= 1;
                system ("CLS");
                printBoard(gameboard, p1, cpu);
                if (p1.p == 9 || p1.p == 19|| p1.p == 29|| p1.p == 39|| p1.p == 49|| p1.p == 59|| p1.p == 69|| p1.p == 79|| p1.p == 89) //checks for boundaries
                {
                    p1.p += 10;
                    system("CLS");
                    printBoard(gameboard, p1, cpu);
                }
                break;
            case 'D':
            case 'd':
                p1.p += 1;
                system ("CLS");
                printBoard(gameboard, p1, cpu);
                if (p1.p == 10 || p1.p == 20|| p1.p == 30|| p1.p == 40|| p1.p == 50|| p1.p == 60|| p1.p == 70|| p1.p == 80|| p1.p == 90) 
                {
                    p1.p -= 10;
                    system("CLS");
                    printBoard(gameboard, p1, cpu);
                }
                break;
            default: 
                cout << "You entered a wrong move. \n";  
            }
    }
    
}

void movementAttribute (Ships & p1)
{
    
    cout << "Please enter agility (only from 0 to 6): ";
    cin >> p1.agility;
    cout << endl;
    
    while(p1.agility < 0 || p1.agility > 6)
    {
        cout << "Too low or too high! PLease enter agility again (only from 0 to 6): ";
        cin >> p1.agility;
        cout << endl;
    }
    
}


And as for the system() thing, I'm ok with using t for this assignment. I will learn about it later, but right now, this assignment is really paramount. Also about the commenting.. i will post a more commented code later on (probably tomm), I'll try my best to make the purpose of the game more evident.
I hate to disrupt this topic but I remember in this forum there is this poster that will point you to a URL why developing games using console is a NO-NO. You should make it GUI-enabled.

Console input is fraught with a lot of possibly human input errors that is hard to capture and code it perfectly. Furthermore it is not visually as appealing as a GUI game :) Worst to worst if you still want console games, he recommend ncurses instead.

Now where is that guy. He seem to have an agenda against any game developer who want to use console hahaha... :)
This is a school assignment I have to do in a console, so yea. Also, this is my first time ever touching a computer language so GUI programming is ways away for me.
Hi halopower67, I am *NOT* against game development in a console. I am just saying somehow that poster seems to be against games developed in console.

As for your explanations it is perfectly valid. I guess it should pacify that guy :P

PS no see him in this forum recently guess pretty tied up at work
It's all good, I hope he doesn't come here complaining lol.
ok, so what i think is that you have some points in game developing. the most important thing is that you have to design it first. (design and define every incremental if you know what i mean)

a game mainly consists of a graphic engine(Drawing engine), GameLoop, AI system, UserInputHandler, Levels.

In here the game loop is an infinite loop that you can even call it the Game actually. it is the controller of the game. its like an Operating System for the game. it is the loop that checks for every thing like time, ammo..etc. and also checks whether any events has occurred. (events such as fire hits, deaths, reborns/spawns..etc. ) I thinks this is what your game lacks of.

AI system is what make the computer moves its players. it is mainly based on pseudo random numbers. the functions you used , srand() and rand() will generate such numbers for you. its not that hard. if this is like a car race thing, you can use Elastic Ribbon AI method. ( the distance between player and computer player works as an elastic ribbon, use Pythagoras method to calculate distance and multiply distance with a random number, then set it as the traveling acceleration, find the rotation using Trigonometry and move )

its best if you implement these as seperate clsses. and all those will be able to plugged out of the game and replaced by a new one. that way u can plugin this to any gui or console or gameboy or anyting... also you can plugin new AI system, a new input system, new game levels whenever you want...

try googling or wikipedia for further infomation on the following keywords,
gameloop , game level, game map, game sprite, game AI, game engine, graphic engine

if you can wait about a month of time, i am working on a full game developing tutorial on my spare time, and it will be finished in another month i hope.
Hi if I'm not wrong, some game developers took the easy way by using expensive commercial software like Maya ?

How it is different from developing game from scratch vs those commercial software for gaming development ?
@sohguanh:
you are saying this because you actually dont know how they do it. what they do is create the models, rigs, animations with maya(my personal fav. is maya and i hate 3dsmax) and then manipulate the maya rig using your game engine. thats how you end up with those machinima and stuff..
most people use 3smax for gaming and maya for movies. but i love maya too much that i use maya for both. you will have to then understand the file system that maya uses to plug it in to your game. creating the whole game inside maya is a total madness because the efficiecy is too low..
a game is NOT GRAPHICS..
(you can create a game with kickass graphics without a doubt. the problem is how to play it.. :P you will need a supercomputer to play it.)
maya , openGL, OGRE, Flash, OSG are some good graphic engines... not game engines. you use them to ceate graphics for the game. its the drawing class(graphics class) that i told you about... its just a fragment of a game.

a game is something far advanced where you have to put a lots of logical thinking. you will need mathematical knowledge to develop a good game.

if you want this to be a 2D game console games, flash games are awsome.. if its 3D game chose some 3d engine that suits you..

if you want sound, you have to do little sound engineering too..

hope your work will be helped by these words...
cheers... :)
Chathura666 thanks. That may have some indication as to what to do. Although more help would be appreciated with maybe more detail, mainly using what I have. And for the next two replies after chathura666... I don't think you guys read the question. You guys are talking about really advanced game programming and design, what I'm trying to do here is just a simple game in a console window as a project for an intro to c++ class
i suggest you to do the game with just using cout (or printf() )...

e .g .
when player attacks, screen will dispaly
player attacks

and the next line could be , the
computer player avoids the attack
or
computer player looses 5 helth

like that...
(what you will see in the game is nothing but words...)

then after you finish the game just with these word things.. you can start putting the relevant things into those places that contains "cout" codes and make the game complete..

i will try to show you it...
copy paste and run these codes... i wrote this just for you... :P hehe... try to understand the concept, what a game really is... in here all the parts are in the same class, because its a very small game.. when things become larger, u will have to move them out...
try this our and tell me...
and have fun...
cheers...

ps.
thanks to you , i got this idea of writing this very simple game as the first introduction to games.. i will complete this game and make it the first tutorial, and the one i am currently working on will be the 2nd tutorial... because its a little bit advanced, but still its a console game...

main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <stdlib.h>
#include <stdio.h>
#include "game.h"

int main()
{
	printf("--BEGINS--\n");

	Game game;
	int status = game.run();

	if(status)
		printf("Error: The game ends with the status of %d\n",status);
	else
		printf("The game ends successfully\n");

	printf("--ENDS--\n");
	return 0;
}


game.h
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
#ifndef GAME_H
#define GAME_H

class Game
{
public:
	Game(void); // constructor
	~Game(void); // deconstructor
	int run(); // start the game ( start the game loop )

	// accessor methods
	int getGoals();
	int getAttempts();

	void setGoals(int g);
	void setAttempts(int a);

private:
	// private methods
	char getInput(void); // get user input
	char getAI(void); // AI thinks what to do
	void update(char goalie, char striker); // update the game status

	// attributes
	int goals;
	int attempts;
};

#endif 


game.cpp
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
#include "game.h"
#include "tools.h"
#include <iostream>

using namespace std;

Game::Game(void)
{
	cout << "Starting a game..." << endl;
	goals = 0;
	attempts = 0;
}

Game::~Game(void)
{
	cout << "Closing the game..." << endl;
}

int Game::run(void)
{
	char command = 'm' ;
	char strike;
	do{
		command = getInput();
		strike = getAI();
		update(command,strike);

	}while(command);
	return 0;
}

char Game::getInput(void)
{
	cout << endl << "Choose your position wisely!" << endl;
	cout << " l : LEFT  r : RIGHT  m : MIDDLE " << endl;
	cout << "Position (Lower Case Letter):";
	char command;
	cin >> command;
	switch (command)
	{
		case 'l':
			cout << "Goalie goes LEFT" << endl;
			break;
		case 'r':
			cout << "Goalie goes RIGHT" << endl;
			break;
		case 'm':
			cout << "Goalie stays MIDDLE" << endl;
			break;
		case 'x':
			cout << endl << "The Goalie quits the game" << endl << endl;
			command = 0;
			break;
		default:
			cout << "Your hesitation can cost your fall!" << endl;
			srand((unsigned int)command);
			command = (char)rand()%3;
			switch ((int)command)
			{
			case 0:
				command = 'l';
				break;
			case 1:
				command = 'r';
				break;
			case 2:
				command = 'm';
				break;
			default:
				command = 'l';
				cout << "Error: command set to LEFT to resolve the error" << endl;
				break;
			}
			break;
	}

	return command;
}

char Game::getAI(void)
{
	char side;

	srand(attempts-goals);
	int s = rand()%3;
	switch(s)
	{
	case 0:
		side = 'l';
		break;
	case 1:
		side = 'r';
		break;
	case 2:
		side = 'm';
		break;
	default:
		side = 'm';
		break;
	}

	return side;
}

void Game::update(char goalie, char striker)
{
	if(goalie)
	{
		char goaliePosition[7] = {0,0,0,0,0,0,0};
		char strikerPosition[7] = {0,0,0,0,0,0,0};
		getNamed(goaliePosition,goalie);
		getNamed(strikerPosition,striker);

		cout << " Goalie goes " << goaliePosition << " and suddenly, "\
 << endl << "the striker strikes it hard to the " << strikerPosition <<\
 ".....and.." << endl;

		if(goalie == striker)
		{
			cout << " Woow, what a wonderful save by the goalkeeper" << endl;
		}
		else
		{
			cout << " What a goal.... " << endl << "the keeper never stood a chance.. "\
 << endl << "right into the "<<	strikerPosition << " of the net.." << endl <<\
 " and Booooom..." << endl;
			goals++;
		}
		attempts++;
	}
}

void Game::setGoals(int g)
{
	goals = g;
}
int Game::getAttempts()
{
	return attempts;
}

int Game::getGoals()
{
	return goals;
}

void Game::setAttempts(int a)
{
	attempts = a;
}


tools.h
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
#ifndef TOOLS_H
#define TOOLS_H

// copy strings
void strcopier(char * dest, char * source,int maxSize)
{
	int i = 0;
	while(source[i] && maxSize)
	{
		dest[i] = source[i];
		maxSize--;
		i++;
	}

	dest[i] = 0;

}

// takes the char and return the corresponding name
void getNamed(char *name,char position)
{
	char left[5] = "left";
	char right[6] = "right";
	char middle[7] = "middle";

	switch(position)
	{
	case 'l':
		strcopier(name, left,4);
		break;
	case 'r':
		strcopier(name, right,5);
		break;
	case 'm':
		strcopier(name, middle,6);
		break;
	}
}

#endif 
UPDATE: Well i reworked the way that the ships are represented on the board visually and logically. I did this because my professor asked me to do this so i have an easier time manipulating the ships later on. In any case, i did that, and now i need help and ideas on how to put boundaries on the board so the ships cant go beyond the board and cant be seen, and also if they go to the border and keep going, they just pop up on the other side of the grid (like a game of pac-man). Also I appreciate any ideas on how i would go about the shooting. According tot he rules that i have to make the game by, ships can only shoot to its sides (lieka real pirate ship) and not the front or the back, also, they can have facing directions, so they can be facing North, East, South, or West.

Ill' show you guys my code to see the changes i made and any questions and suggestions are welcome :)

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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
#include <cstdlib>
#include <iostream>
#include <iomanip>
#include <ctime>


using namespace std;

    struct ships
    {
           int agility; //the amount of moves it has each turn
           int armor;
           int weapons;
           int range;
    };

void printBoard (char[][10]);
void initGrid(char grid[][10], int, int, int, int);
void movement (char gameboard[][10], int&, int&, int&, int&, ships&);
void movementAttribute (ships & p1, ships & cpu);
void cpumovement (char gameboard[][10], int&, int&, int&, int&, ships&);
bool gameOver (ships p1, ships cpu);

int main(int argc, char *argv[])
{
    
    char choice;
    ships p1;
    ships cpu;
    char gameboard[10][10];
    
    p1.agility;
    
    srand(time(0));
    int p1_row = rand()%8+1;
    int p1_col = rand()%8+1;
    int cpu_row = rand()%8+1;
    int cpu_col = rand()%8+1;
    
    
    cout << "\n\n";
    cout << "\t Heyo, wanna play the game? (Y = yes, N = exit): ";
    cin >> choice;
    cout << "\n\n";
    
    
    
    switch (choice)
    {
        case 'Y':
        case 'y':
            //if(gameOver(p1, cpu) = true)
            {
                
                initGrid(gameboard, p1_row, p1_col, cpu_row, cpu_col);
                printBoard(gameboard);
                movementAttribute (p1, cpu);
                movement (gameboard, p1_row, p1_col, cpu_row, cpu_col, p1);
                cpumovement (gameboard, p1_row, p1_col, cpu_row, cpu_col, cpu);
            }
            break;
            
        case 'N':
        case 'n':
            exit(0);
            break;
         
        default:
            cout << "Wrong input. It's either: Y to play, or N to quit.\n";
    }
    
    
         
    system("PAUSE");
    return EXIT_SUCCESS;
}


void initGrid(char gameboard[][10], int p1_row, int p1_col, int cpu_row, int cpu_col) 
{
    for(int r = 0; r < 10; r++)
    {
        for (int c = 0; c < 10; c++)
        {
            if (r == 0 || r == 9)
                gameboard[r][c] = 205;
            else if(c == 0 || c == 9)
                gameboard[r][c] = 186;
            else
                gameboard[r][c] = ' ';
        }

    } 
    
    
    gameboard[0][0] = 201;
    gameboard[9][0] = 200;
    gameboard[0][9] = 187;
    gameboard[9][9] = 188;
    
    
    
    gameboard[p1_row][p1_col] = 193; //single T- player
    gameboard[cpu_row][cpu_col] = 208; //double TT- computer
}

void printBoard (char gameboard[][10])
{
    for (int r = 0; r < 10; r++)
    {
        for (int c = 0; c < 10; c++)
        {
			cout << gameboard[r][c];
        }
        cout << endl; 
    }
}


void movement (char gameboard [][10], int& p1_row, int& p1_col, int& cpu_row, int& cpu_col, ships & p1)
{
    char move;
    
    for(int moves = 0; moves < p1.agility; moves++)
    {
        cout << "Please enter your moves : ";
        cin >> move;
        switch (move)
        {
            case 'w':
            case 'W':
                p1_row = p1_row - 1;
                gameboard[10][10] = ' ';
                gameboard[p1_col][p1_row] = 193;
                system("CLS");
                initGrid(gameboard, p1_row, p1_col, cpu_row, cpu_col);
                printBoard(gameboard);
                break;
            case 'a':
            case 'A':
                p1_col = p1_col - 1;
                gameboard[10][10] = ' ';
                gameboard[p1_col][p1_row] = 193;
                system("CLS");
                initGrid(gameboard, p1_row, p1_col, cpu_row, cpu_col);
                printBoard(gameboard);
                break;
            case 'd':
            case 'D':
                p1_col = p1_col + 1;
                gameboard[10][10] = ' ';
                gameboard[p1_col][p1_row] = 193;
                system("CLS");
                initGrid(gameboard, p1_row, p1_col, cpu_row, cpu_col);
                printBoard(gameboard);
                break;
            case 's':
            case 'S':
                p1_row = p1_row + 1;
                gameboard[10][10] = ' ';
                gameboard[p1_col][p1_row] = 193;
                system("CLS");
                initGrid(gameboard, p1_row, p1_col, cpu_row, cpu_col);
                printBoard(gameboard);
                break;
            default:
                cout << "boom";
        }
    }
    
    
}

void movementAttribute (ships & p1, ships & cpu)
{
    
    cout << "Please enter agility (only from 0 to 6): ";
    cin >> p1.agility;
    cout << endl;
    
    srand(time(0));
    cpu.agility = rand()%6+1;
    
    
    while(p1.agility < 0 || p1.agility > 6)
    {
        cout << "Too low or too high! PLease enter agility again (only from 0 to 6): ";
        cin >> p1.agility;
        cout << endl;
    }
    
}

void cpumovement (char gameboard [][10], int& p1_row, int& p1_col, int& cpu_row, int& cpu_col, ships & cpu)
{
    srand(time(0));
    int s;
    
    for(int moves = 0; moves < cpu.agility; moves++)
    {
            s = rand()%4+1;
            switch (s)
            {
                
                case 1:
                    cpu_row = cpu_row - 1;
                    gameboard[10][10] = ' ';
                    gameboard[cpu_col][cpu_row] = 208;
                    system("CLS");
                    initGrid(gameboard, p1_row, p1_col, cpu_row, cpu_col);
                    printBoard(gameboard);
                    break;
                case 2:
                    cpu_col = cpu_col - 1;
                    gameboard[10][10] = ' ';
                    gameboard[cpu_col][cpu_row] = 208;
                    system("CLS");
                    initGrid(gameboard, p1_row, p1_col, cpu_row, cpu_col);
                    printBoard(gameboard);
                    break;
                case 3:
                    cpu_col = cpu_col + 1;
                    gameboard[10][10] = ' ';
                    gameboard[cpu_col][cpu_row] = 208;
                    system("CLS");
                    initGrid(gameboard, p1_row, p1_col, cpu_row, cpu_col);
                    printBoard(gameboard);
                    break;
                default:
                    break;
            }
    }
            
    
    
}

bool gameOver (ships p1, ships cpu)
{
    bool over = false;
    if(p1.armor < -6 || cpu.armor < -6)
    {
        over = true;
    }
    return over;
}
        


I feel like i did a lot of progress today, because i reworked how the game works and most importantly, i haven't taken my eyes off my computer screens the whole day :p
I did boundary checking on the game but for some reason it is crashing still, any indications as to why it would do that? Im at a loss right now :(

These are the places im doing boundary checking for the player ship and cpu ship:

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
void movement (char gameboard [][11], int& p1_row, int& p1_col, int& cpu_row, int& cpu_col, ships & p1)
{
    char move;
    
    for(int moves = 0; moves < p1.agility; moves++)
    {
        cout << "Please enter your moves : ";
        cin >> move;
        switch (move)
        {
            case 'w': //if "move" is assigned w or W .. move up on the board
            case 'W':
                p1_row = p1_row - 1;
                gameboard[11][11] = ' ';
                gameboard[p1_col][p1_row] = 193;
                system("CLS");
                initGrid(gameboard, p1_row, p1_col, cpu_row, cpu_col);
                printBoard(gameboard);
                if (p1_row <= 0)
                {
                    p1_row = p1_row + 10;
                    gameboard[11][11] = ' ';
                    gameboard[p1_col][p1_row] = 193;
                    system("CLS");
                    initGrid(gameboard, p1_row, p1_col, cpu_row, cpu_col);
                    printBoard(gameboard);
                }
                break;
            case 'a': //if "move" is assigned a or A.. move left on the board
            case 'A':
                p1_col = p1_col - 1;
                gameboard[11][11] = ' ';
                gameboard[p1_col][p1_row] = 193;
                system("CLS");
                initGrid(gameboard, p1_row, p1_col, cpu_row, cpu_col);
                printBoard(gameboard);
                if (p1_col <= 0)
                {
                    p1_col = p1_col + 10;
                    gameboard[11][11] = ' ';
                    gameboard[p1_col][p1_row] = 193;
                    system("CLS");
                    initGrid(gameboard, p1_row, p1_col, cpu_row, cpu_col);
                    printBoard(gameboard);
                }
                break;
            case 'd': //if "move" is assigned d or D.. move right on the board
            case 'D':
                p1_col = p1_col + 1;
                gameboard[11][11] = ' ';
                gameboard[p1_col][p1_row] = 193;
                system("CLS");
                initGrid(gameboard, p1_row, p1_col, cpu_row, cpu_col);
                printBoard(gameboard);
                if (p1_col >= 10)
                {
                    p1_col = p1_col - 10;
                    gameboard[11][11] = ' ';
                    gameboard[p1_col][p1_row] = 193;
                    system("CLS");
                    initGrid(gameboard, p1_row, p1_col, cpu_row, cpu_col);
                    printBoard(gameboard);
                }
                break;
            case 's': //if "move" is assigned s or S.. move down on the board
            case 'S':
                p1_row = p1_row + 1;
                gameboard[11][11] = ' ';
                gameboard[p1_col][p1_row] = 193;
                system("CLS");
                initGrid(gameboard, p1_row, p1_col, cpu_row, cpu_col);
                printBoard(gameboard);
                if (p1_row >= 10)
                {
                    p1_row = p1_row - 10;
                    gameboard[11][11] = ' ';
                    gameboard[p1_col][p1_row] = 193;
                    system("CLS");
                    initGrid(gameboard, p1_row, p1_col, cpu_row, cpu_col);
                    printBoard(gameboard);
                }
                break;
            default:
                cout << "boom";
                break;
        }
    }
    
    
}


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
void cpumovement (char gameboard [][11], int& p1_row, int& p1_col, int& cpu_row, int& cpu_col, ships & cpu) //random cpu ship movement
{
    
    int s;
    
    for(int moves = 0; moves <= cpu.agility; moves++)
    {
            s = rand()%4+1;
            switch (s)
            {
                
                case 1: //if "s" is assigned 1.. move up on the board
                    cpu_row = cpu_row - 1;
                    gameboard[11][11] = ' ';
                    gameboard[cpu_col][cpu_row] = 208;
                    system("CLS");
                    initGrid(gameboard, p1_row, p1_col, cpu_row, cpu_col);
                    printBoard(gameboard);
                    if (cpu_row <= 0)
                    {
                        cpu_row = cpu_row + 10;
                        gameboard[11][11] = ' ';
                        gameboard[cpu_row][cpu_row] = 193;
                        system("CLS");
                        initGrid(gameboard, p1_row, p1_col, cpu_row, cpu_col);
                        printBoard(gameboard);
                    }
                    break;
                case 2: //if "s" is assigned 2.. move left on the board
                    cpu_col = cpu_col - 1;
                    gameboard[11][11] = ' ';
                    gameboard[cpu_col][cpu_row] = 208;
                    system("CLS");
                    initGrid(gameboard, p1_row, p1_col, cpu_row, cpu_col);
                    printBoard(gameboard);
                    if (cpu_col <= 0)
                    {
                        cpu_col = cpu_col + 10;
                        gameboard[11][11] = ' ';
                        gameboard[cpu_col][cpu_row] = 193;
                        system("CLS");
                        initGrid(gameboard, p1_row, p1_col, cpu_row, cpu_col);
                        printBoard(gameboard);
                    }
                    break;
                case 3: //if "s" is assigned 3.. move right on the board
                    cpu_col = cpu_col + 1;
                    gameboard[11][11] = ' ';
                    gameboard[cpu_col][cpu_row] = 208;
                    system("CLS");
                    initGrid(gameboard, p1_row, p1_col, cpu_row, cpu_col);
                    printBoard(gameboard);
                    if (cpu_col >= 10)
                    {
                        cpu_col = cpu_col - 10;
                        gameboard[11][11] = ' ';
                        gameboard[cpu_col][cpu_row] = 193;
                        system("CLS");
                        initGrid(gameboard, p1_row, p1_col, cpu_row, cpu_col);
                        printBoard(gameboard);
                    }
                    break;
                case 4: //if "s" is assigned 4.. move down on the board
                    cpu_row = cpu_row + 1;
                    gameboard[11][11] = ' ';
                    gameboard[cpu_col][cpu_row] = 208;
                    system("CLS");
                    initGrid(gameboard, p1_row, p1_col, cpu_row, cpu_col);
                    printBoard(gameboard);
                    if (cpu_row >= 10)
                    {
                        cpu_row = cpu_row - 10;
                        gameboard[11][11] = ' ';
                        gameboard[cpu_col][cpu_row] = 193;
                        system("CLS");
                        initGrid(gameboard, p1_row, p1_col, cpu_row, cpu_col);
                        printBoard(gameboard);
                    }
                    break;
                default:
                    break;
            }
    }
            


}
Last edited on
Just felt like breaking in to here to clarify a few things:
system(x); calls an external program within yours and should not be used to clear/pause/do anything at all. You should use a library if you want to do certain things, which in the case of Windows, is already supplied to you. The overhead of checking every executable file you try to execute isn't worth it either, generally, because system(x) isn't even that fast.

so GUI programming is ways away for me.

GUI programming - although hard looking and sounding - can be broken down in a lot easier logic then console programs that need to function as real-time applications. GUI programming usually involves events (represented as functions or classes) that are called when the user does something. The rest is just linking and including, really. I have yet to start off with a real program to get the actual codes a safe home in my head, but I have heard of many a programmer that it's not as hard as it seems.

Console programming can be hard. Despite all you might have learned already, that IS a fact. EVERYTHING can be hard (GUI and Graphics programming can be hard, too). Depending on their application, you might choose different of these at different times. Decide HOW you want something done when you want something done.

Sure, this is for a school assignment, which means you have limited time and probably limited options based on the restrictions and specifications, but I just wanted to make that clear.

Further reference:
http://www.cplusplus.com/forum/articles/28558/
Topic archived. No new replies allowed.