Page 4 of 7

Re: Simon Says Game

Posted: July 5th, 2017, 4:15 pm
by Zedtho
I got it to work! https://github.com/Zedtho/Simon-Says
Now for the actual program, Im having a problem with it not actually responding when I click on a square.
Here's what the game should look like at the end http://www.freesimon.org/welcome/
I do not endorse or am affiliated with the website or the people working on it and will not be blamed if you get a virus or something along those lines.

EDIT: It added the sln file as a text document again. Why isnt it working this time? :(

Re: Simon Says Game

Posted: July 6th, 2017, 12:59 am
by albinopapa
This isn't a good way to handle things

Code: Select all

while( duration < 1 )
{
	BLRed = 255;
	BLGreen = 255;
	BLBlue = 0;
	end = std::chrono::steady_clock::now();
	runtime = end - start;
	duration = runtime.count();
	gfx.BeginFrame();
	ComposeFrame();
	gfx.EndFrame();
}
The game locks up on me, because it gets stuck in one of these while loops.

The game is already in a loop, so the BeginFrame, ComposeFrame and EndFrame are being called once per frame already. You need to make iterations last an entire frame and not try to draw and present in a while loop like that.

Try making some functions to separate the code. This will make it easier to find what where problems are and depending on how you write your functions, there could be some code reuse. For instance, a lot of the code you have is very similar, to other sections, try putting that code in a function. Use function parameters to change how the function behaves.

Re: Simon Says Game

Posted: July 6th, 2017, 4:38 pm
by Zedtho
I'll make a more object-oriented version right now! Thanks for the feedback :D

Re: Simon Says Game

Posted: July 6th, 2017, 9:21 pm
by albinopapa
Doesn't have to be OO, but the biggest concern is the begin, compose, end frames being in while loops inside of a loop that already calls those functions.

You should at least figure out how to use enums as a way of implementing a state machine. While in a specified state, the current color pattern is lit, then after a period of time and frames, change states and the color will dim and advance to the next pattern. You can have two states for the computer and player based on turn, and each one of those states can have their own state based on time. This will allow you to control flow of the game across multiple frames.

Re: Simon Says Game

Posted: July 8th, 2017, 1:01 pm
by Zedtho
I'll note that Papa!

I've got a rather odd error this time:

Squares.h file

Code: Select all

class Square
{
public:
	int RedV;
	int GreenV;
	int BlueV;
    int HorizontalLeft;
    int HorizontalRight;
    int VerticalBottom;
    int VerticalTop;	
	Square(int GivenRedV, int GivenGreenV, int GivenBlueV, int GivenHorizontalLeft, int GivenHorizontalRight, int GivenVerticalBottom, int GivenVerticalTop)
	{
		RedV = GivenRedV;
		GreenV = GivenGreenV;
		BlueV = GivenBlueV;
		HorizontalLeft = GivenHorizontalLeft;
		HorizontalRight = GivenHorizontalRight;
		VerticalBottom = GivenVerticalBottom;
		VerticalTop = GivenVerticalTop;
	}
};

I can't figure out how to initialize my 4 squares, RedTopLeft, BlueTopRight, YellowBottomLeft and GreenBottomRight:

Top Part in game.cpp

Code: Select all

Game::Game( MainWindow& wnd )
	:
	wnd( wnd ),
	gfx( wnd )
{
	Square RedTopLeft (50, 0, 0, +100, +5, -5, -100);
	Square BlueTopRight (0, 0, 50, -5, -100, -5, -100);
	Square YellowBottomLeft (50, 50, 0, +100, +5, +5, +100);
	Square GreenBottomRight (0, 50, 0, -100, -5, +5, +100);
}
I also tried to put these initializations in game.h, but that didnt change anything
game.h

Code: Select all

	Square RedTopLeft;
	Square BlueTopRight;
	Square YellowBottomLeft;
	Square GreenBottomRight;
The Debug Output
1>------ Build started: Project: Engine, Configuration: Release Win32 ------
1> Game.cpp
1> Main.cpp
1> MainWindow.cpp
1>Game.cpp(29): error C2512: 'Square': no appropriate default constructor available
1> c:\users\gebruiker\downloads\simon says v3\chili framework 2016\engine\Squares.h(4): note: see declaration of 'Square' (compiling source file Game.cpp)
1>Game.cpp(48): warning C4244: 'argument': conversion from 'time_t' to 'unsigned int', possible loss of data
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

Re: Simon Says Game

Posted: July 8th, 2017, 1:50 pm
by xu2201g
Hi,
the problem is like the error message says those member variables in game.h cant be constructed by default. You defined a specific constructor for the Square class:

Code: Select all

Square(int GivenRedV, int GivenGreenV, int GivenBlueV, int GivenHorizontalLeft, int GivenHorizontalRight, int GivenVerticalBottom, int GivenVerticalTop)
The default constructor is usually a constructor with no parameters:
like classname(){}

I guess you tried to initialize those members in the game constructor here:

Code: Select all

Game::Game( MainWindow& wnd )
   :
   wnd( wnd ),
   gfx( wnd )
{
   Square RedTopLeft (50, 0, 0, +100, +5, -5, -100); //declaring local variables initialized with your specified constructor in Square.h
   Square BlueTopRight (0, 0, 50, -5, -100, -5, -100);
   Square YellowBottomLeft (50, 50, 0, +100, +5, +5, +100);
   Square GreenBottomRight (0, 50, 0, -100, -5, +5, +100);
}
But you re actual creating new local variables cause you placed the type Square in front of those.
And thatswhy the members in game.h are not initialized with these lines.
And so the compiler bitches that those members cant be constructed properly.

You could put those lines in the initializer list of the game.cpp constructor like:

Code: Select all

Game::Game( MainWindow& wnd )
   :
   wnd( wnd ),
   gfx( wnd ),

   RedTopLeft (50, 0, 0, +100, +5, -5, -100), //initializing members stored in game.h with your specific constructor
   BlueTopRight (0, 0, 50, -5, -100, -5, -100),
   YellowBottomLeft (50, 50, 0, +100, +5, +5, +100),
   GreenBottomRight (0, 50, 0, -100, -5, +5, +100)
{
}
Hope that helps. I couldnt test it myself, but it should point you at least in the right direction.

Re: Simon Says Game

Posted: July 8th, 2017, 6:03 pm
by albinopapa
xu2201g wrote:You could put those lines in the initializer list of the game.cpp constructor...
Kudos on the solution and explanation with example. The only thing I'd like to point out is if Square doesn't have a default constructor, you must ( not could ) use the initializer list of any class you have a Square object, just like xu2201g's example. To have the option of delaying initialization, you must at the very least add Square() = default; as the default constructor of class Square.

Re: Simon Says Game

Posted: July 8th, 2017, 6:33 pm
by Zedtho
Thanks a lot for the help! The programming is currently going very smoothly by the way,
it's a lot nicer with a bunch of functions & stuff.

Re: Simon Says Game

Posted: July 8th, 2017, 6:38 pm
by albinopapa
sweet

Re: Simon Says Game

Posted: July 12th, 2017, 5:39 pm
by Zedtho
https://github.com/Zedtho/Simon-Says I uploaded my 3rd version of the game. I can't find the bug in it, which is causing the game to stop.

Edit 1: Found one bug, it should be

Code: Select all

 if(TimeOutForSetColor != 0)TimeOutForSetColor--;
instead of just

Code: Select all

TimeOutForSetColor--;


Edit 2: All functions except for UpdateModel are fully operational and have no bugs. If anything, I would probably be calling those functions wrongly.

Edit 3:

Code: Select all

if (wnd.mouse.GetPosX() > AxisX && wnd.mouse.GetPosX() < AxisX + GreenBottomRight.HorizontalRight && wnd.mouse.GetPosY() > AxisY && wnd.mouse.GetPosY() < AxisY + GreenBottomRight.VerticalBottom && wnd.mouse.LeftIsPressed() && TimeOutForSetColor == 0)
				{
					UserInput.push_back(4);
					SetColor(GreenBottomRight, 0, 30, 0);
					TimeOutForSetColor = 30;
					if (UserInput[UserInput.size() - 1] != ColorPattern[UserInput.size() - 1])
					{
						GameOver = true;
					}
				}
and its YellowBottomLeft equivalent used to say SetColor(BlueTopLeft). Fixed.

Edit 4: With that I fixed the last bug I could easily find. Now come the huge logic problems :(

Edit 5: Used to say if(IndexForColorPattern == ColorPattern.size()-1) , fixed, should be without -1.
Will keep posting bugs I find. Game no longer crashes in Debug mode.