Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

DarkBASIC Professional Discussion / Basic Tic-Tac-Toe game?

Author
Message
Confused1612
14
Years of Service
User Offline
Joined: 9th Feb 2012
Location:
Posted: 10th Feb 2012 02:20
I work full time as an Assistant Network/System Administrator and am going to school to be a Network/System Administrator. I already have it lined up to be promoted to a full Administrator instead of just an assistant for when I graduate in May. Unfortunately, to graduate on time I had to take 5 senior level CIT classes, and they were only offering 6 and one I had already taken so my schedule this semester was set for me.

One class I am taking that I would not have taken if there was an option, is one on game development using DarkBasic Professional. Nothing against it, I am just not as comfortable working with code or programming languages as I am setting group policies or dealing with day-to-day IT issues in an office setting.

The class started two weeks ago, the first week we had to make a "Hello World" program...I didn't have any problems with that and I understood it. Week two, we were thrown right into it and had to make a guessing game where a number is picked at random between 1 and 10 and the user has to guess what it is. The game has to say if you are too low or too high and once you guess it right it keeps your score in a "Top Ten" if you scored well enough.

I read and I read and ended up having to just take two guessing games I found on the internet, use pieces from each to make one program, and then add some personal touches to make it my own...unfortunately I still didn't really understand any of it.

This week, we have to make a tic-tac-toe game where you use a box instead of an x and a circle instead of an o. It needs to keep track of your name and how many moves it took you to win. It is a two player game, so no need for AI...but I am still completely out of my league. I have no idea what is going on. The professor gave a PDF of a game similar to the one she wants but I don't even know enough to make the changes needed to fit the other specs.
Latch
19
Years of Service
User Offline
Joined: 23rd Jul 2006
Location:
Posted: 11th Feb 2012 01:12
Looking at the example you posted, I can see how it's a bit confusing - as there really aren't any comments on what the person is doing in that code. But I think I get the idea of it, and I've programmed tic-tac-toe before with AI.

The process is basically this:

Look at the tic tac toe board as a 3X3 grid. Each space as a location, or an identity on this grid. A winner is determined when three consecutive locations have the same symbol or value in them.

One trick, is to determine what constitutes 3 consecutive locations.

Starting with the basics. You need an array to hold the 9 possible locations (3X3 grid):

dim board(3,3)

Not too hard!

Next you have to think about how a win would occur. The simplest way is to assign a value that represents player 1 or player 2. You then add these values across the 3 consecutive spaces, and if the total matches a certain value, then there is a win.

So if player 1 is assigned a value of 1, and player 2 is assigned a value of -1 and 0 represents an open square, then you just add up the squares in a row, column, or diaganol and the value determines a winner.

3 = player 1 wins
-3 = player 2 wins

To set up your rows, columns, etc. to determine a win, you can add them on the spot after each play. We decide what square the array represents as follows



After a play is made, you add up the different directions that include the square you moved to and decide if there is a win. The corners have 3 possible directions, the spaces in between the corners have 2 possible directions, and the center square has 5 possible directions.

That's the bare bones of the logic.

You don't have to add the values, you could check the values in each array box and as soon as you hit the opposite players value, you can stop the check and eliminate that direction from any future checks in the game because there is no way to win in that direction. But that may be too tricky to figure out if you are just beginning to program. So "brute force" with adding each direction would probably suffice.

Enjoy your day.
smerf
21
Years of Service
User Offline
Joined: 24th Feb 2005
Location: nm usa
Posted: 11th Feb 2012 18:18
quite frankly theres a million different ways to code something and achieve the same result. hers a very simple concept 4 u


A child's dream never dies.
Confused1612
14
Years of Service
User Offline
Joined: 9th Feb 2012
Location:
Posted: 12th Feb 2012 02:42
I think I am really starting to get it...but I am trying to set it up so that the array that stores the values of who put their "x" or "o" where is reset after each game. Using Google I found the clear array command but it appears that it doesn't work. Here is the "code" I was trying:


I have it placed at the beginning of the actual game, so when I give the option to play again I go back to this step and clear the array so it is all zero's again. At least that is what I am trying to do.

Well, to give a better understanding I will post all I have right now, but as I said it isn't done because I am stuck on this point. After this I think I might know how to finish it though if I can get this to work.



Also, I know I don't have to increment the line a little bit at a time when drawing the board, I just think it looks better, kinda like actually drawing it on a chalk board or something. Once I get it done I am planning to TRY to add chalk sounds when drawing the lines.
zeroSlave
17
Years of Service
User Offline
Joined: 13th Jun 2009
Location: Springfield
Posted: 12th Feb 2012 06:21 Edited at: 12th Feb 2012 06:32
To clear it, you could just:


My green thumb grew the tree my Trojan War horse was crafted from. With roses in our pockets we rally round the tombstones. Ashes to ashes, we all fall down.
Confused1612
14
Years of Service
User Offline
Joined: 9th Feb 2012
Location:
Posted: 12th Feb 2012 07:48
Can anyone tell me why this isn't working?



Unless I am just completely wrong, it should draw a diagonal line in the first box if you click anywhere on the screen, however it isn't working. Thanks in advance for any help anyone can give.
Broken_Code
15
Years of Service
User Offline
Joined: 20th Aug 2010
Location: Bremen, Germany
Posted: 12th Feb 2012 20:28
It's because you've written:


It's easy to think that might mean wait for mouseclick but actually it means wait for delay, delay=mouseclick() and as mouseclick()=0 when the code is here it waits for 0 milliseconds.
Try this instead:


So now it says: do this: wait 1 millisecond until mouseclick is not equal to 0 which is what I think you wanted?

Also, note the:
sync
sync
this is because there is a buffer between the program and the screen; when sync is called, the contents of the buffer is sent to the screen and then program's visual data is sent to the buffer, ready for the next sync. As the buffer is empty when you call the first sync the screen will be blank, and the buffer filled with the data you wanted to show! So when you're not using a loop you must call sync twice the first time to see anything. Here's that same code in a loop, just to show you:



Hope some of that helps!
BC
Broken_Code
15
Years of Service
User Offline
Joined: 20th Aug 2010
Location: Bremen, Germany
Posted: 12th Feb 2012 20:31
Just adding a mailback!

BC
Confused1612
14
Years of Service
User Offline
Joined: 9th Feb 2012
Location:
Posted: 13th Feb 2012 21:03
Here is my "final" code. All I have left is to work out bugs.



The only problem (that I am seeing anyway) is that when it asks me if I want to play again it doesn't work. Thanks in advance for any help.
Latch
19
Years of Service
User Offline
Joined: 23rd Jul 2006
Location:
Posted: 14th Feb 2012 03:14 Edited at: 14th Feb 2012 03:33
Not bad!

There's a bit of redundancy, but that'll clear up with practice and experience. One glaring problem with your function:



Is the goto DrawBox - which is forcing (or attempting) a jump out of a function to a subroutine in the bulk of the code. Think of a function as it's own little universe where everything related to the function only happens within the function. The only way in or out, is to pass a value to a parameter or to return a value from the function after it has been worked on. For example, here's a simple add numbers function:



The a and b are the parameters, and result is the value returned. You don't always have to pass parameters so, relating this to your function, instead of trying to jump out of the function using goto, we can return the result of pressing Y or N and use that to control the program logic:



Overall, you may want to change the order of your program. For example



Also, try and think about how maybe you could use a loop or reapplicable code to repeat similar or the same actions and lessen the "Brute Force" method of writing out every combination of program possibilities.

Like drawing X's or O's and checking their position. You could have 1 routine that returns the board(x,y) for any position instead of mapping all of the posiible positions.

For example, if the screen only displayed 1 pixel for each box, then it would be real simple to find what box the mouse was over:



If your tic tac toe squares are bigger, say 64X64, then all you need is a little math to figure things out



That simple code can eliminate a huge number of your if then checks. So if you can understand what I did, then you should be able to adapt it to draw your circles or xs and get the board() position and value's you need.

Note: The +1 I add to the x and y values is because the pixels on the screen start at 0,0 but your board(x,y) starts at 1,1. So based on the example of each square being 64x64 pixels, at position mousex = 127, and mousey = 171,

posx=127/64 = 1
posx = 1+1 = 2

posy=171/64 = 2
posy = 2+1 = 3

final
board(2,3)

You'd also have to take into account an offset (the position on the screen where you board is drawn). If the left edge of your board starts at 150, then you' have to subtract that from the mouse x position before doing your division.

posx=((mousex()-xoffset)/pixel size)+1
if posx > 0 and posx < 4
continue...

Enjoy your day.

Login to post a reply

Server time is: 2026-07-09 17:43:08
Your offset time is: 2026-07-09 17:43:08