need help creating a tictactoe like game
-
I want to be able to move the joystick along a 3x3 matrix of squares numbered from 0 to 8 , and where the joystick lands it will revel the number on the given square.
the matrix:
|0|1|2|
|3|4|5|
|6|7|8| -
I think you need to be a little more specific with your questions. What exactly is it that you want help with?
I think the first thing you need to do is to decide on how you want to represent your square matrix in the code. Perhaps a single array from [0] to [8], or perhaps a two-dimensional array from [0][0] to [3][3]? For tic-tac-toe, the array could contain a number that would be 0 for neutral squares, 1 for squares taken by player 1, and 2 for those taken by player 2.
You need some kind of index that points to the currently selected square as well. Then you will need to read the user input and change the index accordingly.
Then you should probably get some code down to draw the square on the screen. A loop or a set of loops running through all the squares and drawing them in the correct location. Add an if statement to check if the current square is the one that the index is pointing to and in that case display the number on top of it.
Something like that. Feel free to ask any questions, but the more specific the questions are the easier it is to answer them in a way that will be useful for you!
-
as far as coding some thing like this in C/Perl it's a trivial problem. My issue is with the Switch controls. for example how do I interact with multiple buttons. when I try reset the game with a button on screen, the reset button is pushed many times, instead of just ones. It would be nice if there was some example program that could show me how to interact with 3 buttons on screen.
-
There is nothing about the Switch controls that is fundamentally different from any button - they are simply 1 if being pressed, and 0 if not.
The reason the button is "pushed many times" I imagine is because you have something like this in your code:
if c.a then // reset endif
This will always be triggered for as long as you hold the A button - since while you hold the A button, c.a is true.
You will need to implement some sort of gate or "lock" system for the button you want to press. If dealing with a single button, a single variable is all that is necessary:
press = false // in the main loop, when you want to execute the button press: if c.a and !press then press = true endif
Since "press" has to be false for the if statement to happen, and we immediately make it true upon entering the if, it cannot happen again until press is false. This could be done with something like:
// If A is not pressed, make press false again if !c.a then press = false endif
This is the basic concept, but of course to do this for multiple buttons gets a bit silly since you'll need a gate variable for each button - so instead, we can keep track of the whole controller state on the previous frame. Consider this:
// Global controls variables are helpful so we can access them anywhere in the program c = controls(0) // Will store the current controller state prevc = c // Will store the previous state of the controller (on the previous frame) loop // Update the previous and current controller vars prevc = c c = controls(0) if c.a and !prevc.a then // this will only happen ONCE when A is pressed endif repeat
This is really neat, since you don't have to have all sorts of "press" variables cluttering your program. Think of it like this:
if button and !prevbutton then // Button has been initially pressed endif if !button and prevbutton then // button has been released endif
To get multiple buttons to give the same behaviour, you could create something like this:
pressed = (c.a & !prevc.a) or (c.b & !prevc.b) or (c.x and !prevc.x) if pressed then // This happens once, when either A, B or X are pressed. endif
Hope this helps! Let me know if you need clarification.
-
Hi Dave, thanks for your reply, I will try and implement what you described.
-
is it the same concept when dealing with the touch screen?
-
Yes it is the same principal. You have to track the state of your buttons to know when they have been pressed and released again
-
Thanks, that worked