Coding Journal/Introduction of Sorts

The Partridge Family were neither partridges nor a family. Discuss.
Post Reply
User avatar
neptunemermaid
Posts: 17
Joined: January 25th, 2015, 7:10 pm
Location: BC, Canada

Coding Journal/Introduction of Sorts

Post by neptunemermaid » August 6th, 2017, 11:33 pm

Hey guys, I'm Crystal. Other usernames around the web include cistle, vanillahearts62, and zerocrys.

I'm a comp sci student just starting out. I have done a couple courses in Java and will be continuing in C++ in the near future. I've posted on here before I started going to school, just learnin' on my own. I've done the tutorials for the poo game, except it became a smiley face eating neon coloured hotdogs. That was a couple years ago, but as I am in school learning all the details, I am getting much more comfortable with things. I have a lot more background knowledge thanks to Java, so now I'm just in the process of translating it over to C++ because lets face it, C++ is the bomb. I'm really intrigued by the Arkanoid game, but I think it might be a little bit too advanced for me at the moment. I'll try some other tutorials. Do any of you have anything to suggest?

As I've never done a formal introduction, here is some information about me.
I am from Canada, specifically the west coast. I'm almost 30 (tears) and I have a bachelors of science degree in psychology, with the science part being neurobiology. I'm interested in robotics, collecting rocks, video games (very stoked for Star Citizen 3.0), oil painting, and reading.

I've come back to Planet Chili because I want to make some friends and get better at C++. I figured this is one of the better resources out there that I have found, and Chili's tutorials have been really helpful.

As to make this more of a journal, I need to set some goals.

I think I'm going to revisit the poo game and really polish it up. Start from the beginning. Right now the graphics look like some crack head programmed it while high. I want to make it into something I'm at least sorta proud of showing other people besides my mom.

Another goal would be to complete my current course (I'm doing it through distance this time around, so I don't have any classmates, yay!) so I can move onto the next Java course, which then I can do the C++ course.

I also want to understand the framework. There's the .h files and such, and I have no clue what those all mean. I'm used to Java where you have your main method and some other classes. So I want to be able to learn more about each component of the framework and how it all comes together.

Cheers

albinopapa
Posts: 4373
Joined: February 28th, 2013, 3:23 am
Location: Oklahoma, United States

Re: Coding Journal/Introduction of Sorts

Post by albinopapa » August 7th, 2017, 2:28 am

Welcome to the forums, for C++ and amateur game dev, you've come to the right place.

Impressive background coming in. Programming should be easy enough for you. Once you know one language, you have a pretty good understanding of most all. C++ is freedom and responsibility. Languages like Java and C# are I believe managed languages taking care of resource management for you. C++ gives you more fine grained control of the lifetime of your resources. The file layout is usually split up into two, a header and a source file.

The header file ( .h ) is usually where the interface lives. It tells the compiler and users what methods and members are public for them to use and which are private and only objects of that class can use.

The source file (.cpp) is usually where the magic happens. This is where you put all your method definitions.

Give it time and practice, you'll be fine. Need any help, come ask us.
If you think paging some data from disk into RAM is slow, try paging it into a simian cerebrum over a pair of optical nerves. - gameprogrammingpatterns.com

User avatar
Zedtho
Posts: 189
Joined: February 14th, 2017, 7:32 pm

Re: Coding Journal/Introduction of Sorts

Post by Zedtho » August 7th, 2017, 10:41 am

Albinopapa wrote: Need any help, come ask us.
I can confirm that, they're really helpful here. If you have a short question you can also check out the Discord channel. https://discordapp.com/invite/apdsr56

Good luck learning C++!

User avatar
chili
Site Admin
Posts: 3948
Joined: December 31st, 2011, 4:53 pm
Location: Japan
Contact:

Re: Coding Journal/Introduction of Sorts

Post by chili » August 8th, 2017, 1:53 pm

These threads are always a good time. Welcome to the forum m8

As for the .h/.cpp stuff, I give more details on that later on in the Beginner series (check Compiler Confessions & Circular Dependency videos).
Chili

User avatar
neptunemermaid
Posts: 17
Joined: January 25th, 2015, 7:10 pm
Location: BC, Canada

Re: Coding Journal/Introduction of Sorts

Post by neptunemermaid » August 8th, 2017, 7:21 pm

Thanks guys. I think that I'll have to restart the series. Now that I know the basics a lot better, I'm expecting it to go smoother. Hopefully I can make something cool in the near future.

User avatar
neptunemermaid
Posts: 17
Joined: January 25th, 2015, 7:10 pm
Location: BC, Canada

Re: Coding Journal/Introduction of Sorts

Post by neptunemermaid » July 13th, 2018, 4:59 am

Coded TicTacToe. Putting here for reference


TicTacToeGame.cpp

Code: Select all

#include "TicTacToeGame.h"
#include <iostream>

using namespace std;
char board[3][3] = { '0', '1', '2' };
void TicTacToeGame::playGame()
{
	clearBoard();
	char board[3][3] = { '0', '1', '2' };
	const char player1 = 'X';
	const char player2 = 'O';
	char currentPlayer = player1;


	bool isDone = false;
	int turn = 0;

	// inner game loop
	while (isDone == false)
	{
		printBoard();

		// get coordinates
		int x = getXCoord();
		int y = getYCoord();

		// place a marker
		if (placeMarker(x, y, currentPlayer) == false)
		{
			cout << "There's already something there\n";
		}
		else
		{
			turn++;
			if (checkForVictory(currentPlayer) == true)
			{
				cout << "Game Over! Player " << currentPlayer << "has won!\n";
				isDone = true;
			}
			else if (turn == 9)
			{
				cout << "Tied game!\n";
				isDone = true;
			}
		}

		// switch players
		currentPlayer = (currentPlayer == player1) ? player2 : player1;
	}
}

	int TicTacToeGame::getXCoord()
	{
		bool isInputBad = true;

		int x;

		while (isInputBad == true)
		{
			cout << "Enter the X coordinate: ";
			cin >> x;

			if (x < 1 || x > 3)
			{
				cout << "Invalid Coordinate!\n";
			}
			else
			{
				isInputBad = false;
			}
		}
		return x - 1;
	}

	int TicTacToeGame::getYCoord()
	{

		bool isInputBad = true;
		int y;
		while (isInputBad == true)
		{
			cout << "Enter the Y Coordinate\n";
			cin >> y;

			if (y < 1 || y > 3)
			{
				cout << "Invalid coordinate\n";

			}
			else
			{
				isInputBad = false;
			}
		}
		return y - 1;
	}

	bool TicTacToeGame::placeMarker(int x, int y, char currentPlayer)
	{
		if (board[y][x] != ' ')
		{
			return false;
		}
		board[y][x] = currentPlayer;
		return true;
	}

	bool TicTacToeGame::checkForVictory(char currentPlayer)
	{
		// check rows
		for (int i = 0; i < 3; i++)
		{
			if ((board[i][0] == currentPlayer) && (board[i][0] == board[i][1]) && (board[i][1] == board[i][2]))
			{
				return true; // won the game

			}
		}

		// check columns
		for (int i = 0; i < 3; i++)
		{
			if ((board[0][i] == currentPlayer) && (board[0][i] == board[1][i]) && (board[1][i] == board[2][i]))
			{
				return true; // won the game
			}
		}


		// check top left diagonal
		for (int i = 0; i < 3; i++)
		{

			if ((board[0][0] == currentPlayer) && (board[0][0] == board[1][1]) && (board[1][1] == board[2][2]))
			{
				return true; // won the game
			}
		}


		// check top right diagonal
		for (int i = 0; i < 3; i++)
		{
			if ((board[2][0] == currentPlayer) && (board[2][0] == board[1][1]) && (board[1][1] == board[0][2]))
			{
				return true; // won the game 

			}
			return false;
		}
		return false;
	}

	void TicTacToeGame::clearBoard()
	{
		//Empties the board
		for (int i = 0; i < 3; i++)
		{
			for (int j = 0; j < 3; j++)
			{
				board[i][j] = ' ';
			}
		}
	}

	void TicTacToeGame::printBoard()
	{
		//formatting
		cout << endl;
		cout << " |1 2 3|\n";
		for (int i = 0; i < 3; i++)
		{
			cout << " -------\n";
			cout << i + 1 << "|" << board[i][0] << "|" << board[i][1] << "|" << board[i][2] << "|\n";
		}
		cout << " -------\n";
	}
TicTacToeGame.h

Code: Select all

#pragma once

class TicTacToeGame
{
public:
	TicTacToeGame() = default;
	void playGame();

private:
	// xy coord from user
	int getXCoord();
	int getYCoord();

	// places a marker. If false, couldn't place
	bool placeMarker(int x, int y, char currentPlayer);

	// returns true if winner
	bool checkForVictory(char currentPlayer);

	// clear board
	void clearBoard();

	// prints the board
	void printBoard();


	char board[3][3] = { '0', '1', '2' };
};

main.cpp

Code: Select all

#include <iostream>

#include "TicTacToeGame.h"
using namespace std;

int main()
{
	bool isDone = false;

	TicTacToeGame game;

	while (isDone == false)
	{
		game.playGame();

		cout << "Do you want to play again? Y/N";
		char input;
		cin >> input;

		if (input == 'N' || input == 'n')
		{
			isDone = true;
		}
	}

	system("PAUSE");

	return 0;
}

albinopapa
Posts: 4373
Joined: February 28th, 2013, 3:23 am
Location: Oklahoma, United States

Re: Coding Journal/Introduction of Sorts

Post by albinopapa » July 13th, 2018, 6:10 am

Grrrr, why you no listen, I say zip project and upload or put on GitHub and you still no listen.

All jokes aside, seriously, why make others create a project and associated files, then copy and paste this code over to their project?
If you think paging some data from disk into RAM is slow, try paging it into a simian cerebrum over a pair of optical nerves. - gameprogrammingpatterns.com

User avatar
neptunemermaid
Posts: 17
Joined: January 25th, 2015, 7:10 pm
Location: BC, Canada

Re: Coding Journal/Introduction of Sorts

Post by neptunemermaid » July 13th, 2018, 3:58 pm

Its mostly for me as a backup. Also I'm not sure if there are additional files from Windows Visual Studio or if I can just use these three files and have it work. I'm still learning the IDE and what everything means (such as .sln files).

albinopapa
Posts: 4373
Joined: February 28th, 2013, 3:23 am
Location: Oklahoma, United States

Re: Coding Journal/Introduction of Sorts

Post by albinopapa » July 13th, 2018, 5:30 pm

Ah, I see. Then I'll explain a few things. The .sln file is mostly a shortcut to the .vcxproj file. The .vcxproj file is the configuration or settings file. It tracks all settings for your project including which files are part of your project. There should be two or three vcxproj files, .vcxproj, .vcxproj.user and vcxproj.filter. The filter one keeps track of some organization in Solution Explorer if you create new filters. Not sure of the purpose of the .user one.

That being said, the structure of a project to be shared would be:
  • In the solution folder ( \chili_framework\ ) you need the .sln file.
  • In the project folder ( \chili_framework\Engine ) you need all .h, .cpp, .vcxproj files.
  • You would also need any sprite, sound and/or any other files that you add and your project relies on in the folders your project expects them. ( \chili_framework\Engine\Sprite or \chili_framework\Engine\Audio ).
Items that are not needed when sharing your project:
  • The .vs and/or the .git folder in the solution folder
  • The Debug folders in the solution and project folders
  • The Release folders in the solution and project folders
  • The x64 folders in the solution and project folders
  • Any other files not mentioned above
If you think paging some data from disk into RAM is slow, try paging it into a simian cerebrum over a pair of optical nerves. - gameprogrammingpatterns.com

Post Reply