Jump to content
  • Advertisement
Sign in to follow this  
TheNobleOne

My first game

This topic is 5434 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

An array declared as foo[9] has 9 elements, numbered 0 to 8.

Trust me from years and years of programming experience, you want to work with the 0-8 range internally in all your math, and only convert to 1-9 when interacting with the user:

cin >> where; // user enters 1-9 for location
where -= 1; // convert that to 0-8
row = where / 3; column = where % 3;
// That cleanly results in this layout:
// 0 1 2
// 3 4 5
// 6 7 8
// See how simple the math looks. Later:
for (int loc = 0; loc < 9; loc++) {
// looping from 0 to 8 inclusive
cout << board[loc]; // and whatever other formatting
if (loc % 3 == 2) cout << endl; // wrap at the end of each row
}

Share this post


Link to post
Share on other sites
Advertisement
ok that makes sense. I knew 9 gave 0 - 8 but I guess when I was working my code I glazed over that. However, I am reworking my code to me more efficient and it is actually starting to help me understand when pointers should be used and when not to use them.

However, I have some bugs to work out while refactoring the code.
I seems bugs are popping up here and there while refactoring but it is a good learning experience. Because as they pop up it lets me stop and think and fix/alter the logic of the code. Over all I am glad I started with a simple game like this without even thinking of graphics API.

I also can put in a little hint for the other noob game programmers out there... Learn C++ first and then write a game using C++ but no graphics api or anything. Just do a simple console app based game. It will enlighten you very much and will teach you more about the way C++ works then any book can ever say. Much knowledge about using C++ comes from actually using the language in your own examples rather then just the books examples which don't always show practical applications of the language. Also another tip before you ask here on the forums what is wrong with your code sit back and look at it. Read your code in pain english and think about it. Then tweak where you think something should be tweaked and then test. Do this over and over and over and you will learn something that not even the forums can tell you. The things they tell you here will sink in much better if you discover it for yourself.

You can't start with a huge game and expect to learn well. Start simple then move up and up and up. The simple stuff will teach you C++ once you know that you can think about graphics api's.

Share this post


Link to post
Share on other sites
Well I am not technically a beginner. I am actually a comp sci major. However, I am new to C++ as in school we use java and normally i use C#. But in my own interest I want to learn C++ and game development and hope I can get a job in this field. So I will make sure I save printouts of all my games I write so I have something to show when that time comes.

However, I am liking to work in C++ the more and more I use it.

Share this post


Link to post
Share on other sites
Sorry for the double post but I want to seperate this from everything else so to say.

Well I fully refactored the code. Working great. I took some of your advice. All my calculations using the array now are done including [0]. Then when the user presses 1 for positon one I calcuate that as possition 1 - 1 or possition 0 on the board. I also added a condition that ends the game when there is a tie.

Also I am using pointers where I feel they are necessary. For instance wherever I want to reference the actual value of a variable and not the counter I use a pointer as a function param and send it the address of a variable. What I like about that is it allows very easy passing of a variable from one function into another and back again without haveing to have the function return a value. Here is the code. Please feel free to look at the code and see if you see something that I missed or did wrong.

Oh and btw I am using #include <stdlib.h> because the mingw compiler libs seem not to use the c prefix in front of it.


#include <iostream>
#include <stdlib.h>

using namespace std;

// Prototypes
void DrawCurrentBoardState(char *n);
void StartGame(char *n);
void CheckForWin(char symbol, char *n, bool *q);
void PlayerXTurn(int *pos, char *n);
void PlayerOTurn(int *pos, char *n);

int main(void)
{
// Declarations
char boardData[9];

for(int i = 0; i < 9; i++) // initialize boardData elements
{
boardData = ' ';
}

StartGame(boardData); // start the game
system("PAUSE"); // pause at end of game to keep window open
return 0;
}

void DrawCurrentBoardState(char *n)
{
for(int i = 0; i < 9; i++) // dispaly board data
{
// checks to see if the value is a multiple of three by
// using modulus to make sure there are no remainders
// this allows the drawing of the game board to be exact
// tic tac toe dimensions
if((i + 1)%3 != 0 || i == 0)
{
cout << n << "|";
}
else
{
cout << n << "\n";
}
}
cout << "\n";
}

void StartGame(char *n)
{
bool running = true;
int possition;

cout << "Welcome to TextTicTacToe!" << "\n" << "\n";

DrawCurrentBoardState(n); // display the most current boards data

while(running)
{

PlayerXTurn(&possition, n);

CheckForWin('X', n, &running);

if(running == false) // if win conditions were met
{
continue;
}

PlayerOTurn(&possition, n);

CheckForWin('O', n, &running);

if(running == false) // if win conditions were met
{
continue;
}
}
}

void CheckForWin(char symbol, char *n, bool *q)
{
// check for win
if(n[0] == symbol && n[1] == symbol && n[2] == symbol)
{
cout << "Player " << n[0] << " has won the game." << "\n";
*q = false;
}
else if(n[3] == symbol && n[4] == symbol && n[5] == symbol)
{
cout << "Player " << n[3] << " has won the game." << "\n";
*q = false;
}
else if(n[6] == symbol && n[7] == symbol && n[8] == symbol)
{
cout << "Player " << n[6] << " has won the game." << "\n";
*q = false;
}
else if(n[0] == symbol && n[3] == symbol && n[6] == symbol)
{
cout << "Player " << n[0] << " has won the game." << "\n";
*q = false;
}
else if(n[1] == symbol && n[4] == symbol && n[7] == symbol)
{
cout << "Player " << n[1] << " has won the game." << "\n";
*q = false;
}
else if(n[2] == symbol && n[5] == symbol && n[8] == symbol)
{
cout << "Player " << n[2] << " has won the game." << "\n";
*q = false;
}
else if(n[0] == symbol && n[4] == symbol && n[8] == symbol)
{
cout << "Player " << n[0] << " has won the game." << "\n";
*q = false;
}
else if(n[2] == symbol && n[4] == symbol && n[6] == symbol)
{
cout << "Player " << n[2] << " has won the game." << "\n";
*q = false;
}
else if(n[0] != ' ' && n[1] != ' ' && n[2] != ' ' && n[3] != ' ' &&
n[4] != ' ' && n[5] != ' ' && n[6] != ' ' && n[7] != ' ' &&
n[8] != ' ')
{
cout << "A Tie." << "\n";
*q = false;
}
}

void PlayerXTurn(int *pos, char *n)
{
cout << "Player X which possition would you like to take 1 - 9" << "\n";
cin >> *pos;

if(n[*pos - 1] == ' ')
{
n[*pos - 1] = 'X';
DrawCurrentBoardState(n);
}
else
{
cout << "Space already taken." << "\n";
DrawCurrentBoardState(n);
PlayerXTurn(pos, n);
}
}

void PlayerOTurn(int *pos, char *n)
{
cout << "Player O which possition would you like to take 1 - 9" << "\n";
cin >> *pos;

if(n[*pos - 1] == ' ')
{
n[*pos - 1] = 'O';
DrawCurrentBoardState(n);
}
else
{
cout << "Space already taken." << "\n";
DrawCurrentBoardState(n);
PlayerOTurn(pos, n);
}
}






[Edited by - TheNobleOne on October 1, 2004 2:58:16 PM]

Share this post


Link to post
Share on other sites
The functions PlayerXTurn and PlayerOTurn are very similiar.. why don't you create a seperate function and pass the symbol as parameter?

Instead of checking all the fields you could increment a variable and check if it equalls the possible number of turns.. (This is just they way I would do it.. But your methode is also valid :) )

Why are you passing Position as a pointer to PlayerXTurn/PlayerOTurn? Pos is initialised in the function and it's not being used outside the function.

Have a look at passing parameters by reference. You could change the CheckForWin function to take q as a reference instead of a pointer(cleans up your code)

Good luck :)

Share this post


Link to post
Share on other sites
Well the reason I passes possition as a pointer was to modify the value in that memory address of possition. possition is defined in the StartGame function but I need to use it in other functions. However it just happens that all functions are called by start game so it is just quicker then making all the other functions return a value especially since some functions alter multiple values.

I have two different functions for PlayerXTurn and PlayerYTurn to help in my mind explain that they are two seperate entities playing the game. Just that 2 functions make it a little more obvious.

As far as passing by reference instead of using pointers. I personally like pointer notation. They are flexible and it is just one way to pass something around by reference. Some people may not prefer to read pointer notation I do. Plus if I am correct pointers can give some more control over memory then just passing a value by ref.

The code is not very well commented and I will do that as i am going to make a code portfolio for when I finish my degree. It may help me get a job. Also, I am going to make a consistency change. My prototype for StartGame is

void StartGame(char *n);

however my function def says

void StartGame(char n[])

I will make that read

void StartGame(char *n)

Share this post


Link to post
Share on other sites
Quote:
Original post by TheNobleOne
Well the reason I passes possition as a pointer was to modify the value in that memory address of possition. possition is defined in the StartGame function but I need to use it in other functions. However it just happens that all functions are called by start game so it is just quicker then making all the other functions return a value especially since some functions alter multiple values.


You're right about the use of pointers but I refactored your code and if you would just remove the ' int position' from your startgame code and declare it in PlayerXTurn/PlayerOTurn there is no need for passing it around. You are using initialising it with some user input, then you use Pos as an index to your array.. you don't need the value after you've used it as array index. It would make sence to declare positon in Startgame and pass it as a pointer if you would use the value after the function returns(and changed the pointer) But you're not using position in startgame so why pass it as a pointer? :)

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Quote:
Mode13h


Oh good god! Welcome to 1992. Use DirectX or OpenGL. It's easy.

Share this post


Link to post
Share on other sites
Quote:
Original post by TheNobleOne
Well I am not technically a beginner. I am actually a comp sci major. However, I am new to C++ as in school we use java and normally i use C#.


O_O

Java and C# use zero-based arrays too, as a comp sci major you really ought to be familiar with them, and the standard mathematical tricks that go along with them.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!