Thread: Tic-Tac-Toe!
View Single Post
Old
Dameria
Senior Member
 
Dameria's Avatar
 
Status: Offline
Posts: 978
Join Date: Sep 2009
Location: United States
Default 11-30-2015, 09:06 PM

Hello taeweas and welcome to the forums!

First off, I have to apologize for the way that I programmed this game. As an undergraduate college student about to get his degree in Computer Science, I am wincing as I look back at this programming. It's disgusting code-wise; completely unorganized, inefficient, and non-intuitive. Back when I made this I had little understanding of object oriented code design. Enough of my self-pity though, I'll try to help you understand what I did.

First of all, in my program if you scroll down to the bottom of the Events board you can see that I have an event with the condition "When the world starts, do" followed by the method "scoreboardBackground.setNames". To see this method, you have to maximize the "Scoreboard" object group in the objects window under the Play button. In this folder you will see an object "scoreboardBackground" that you can click on. In the window directly below the objects window you should see the text "scoreboardBackground's details". This is the details window. There should be 3 tabs under this text: properties, methods, and function. Click on "methods" and you will see all of the methods available for this object. I chose to create a method under this object and call it "setNames", you should see it at the top of the methods tab. Press the "edit" box next to the method to open the code.

In the scoreboardBackground.setNames method you can see the code I used to ask the user to set their name using the message boxes when you first run the program. At the very bottom of this method you will see a command to run "world.randomTurn". That randomTurn method is unique in that it is the only global method that I created (for some reason). If you click on the "world" object at the very top of the objects section and click to edit the "randomTurn" method in the world's methods section, you can see the strange code that I created to select the first player's turn. It basically chooses an integer, either 0 or 1 (for some reason I had it generate a 3 or 4, then subtracted by 3??? I'm strange) and assigns that number to the global variable "playerTurn" (found in the "world" object's properties tab). The method now decides that it's O's turn if a 0 is randomly generated or X's turn if a 1 is generated. It then changes the text property of the "turnText" object (found in the object folder "PlayersTurn") to either O or X, whichever "world.playerTurn" is randomly set to. This text is displayed in the gray "Players Turn" square of the world.

If you thought that was the confusing part, you were wrong. If you look at the events, you will see that every clickable square has an event that is triggered when said square is clicked, each calling a different method.
In hindsight, I could have made this into a single event with a single global method by creating an object list through the world's properties tab -> create new variable -> Object + checkbox for List, but I didn't learn how to do this until later in my Alice days.
Anyways... each of these methods pretty much do the same thing, they set the corresponding text within the box to either O or X, depending on whose turn it is by using "world.playerTurn" as the conditional, and then it either calls the method "winText.Player X Won" or "winText.Player O Won!" (don't ask my why O has an exclamation at the end while X doesn't, I don't know why. Maybe I like player O more?). These method names are deceiving though because they don't actually mean that the player has won, but rather it is a method to check to see if the player won. Within these methods are "if" condition blocks checking every single possible combination that would indicate a win; so any diagonal, horizontal, or vertical combination of that player's symbol (O or X) would indicate a win, I think there are 8 possible combinations. If a win is detected, it will show the winning text and background that has been hiding this entire time, flash the winning texts, increment the winner's score, and run the method "winText.playAgain" to ask you if you want to play again.

Sorry about the bad code and for this giant mess of a post. I hope it wasn't too confusing and that it helps you make your own game. I am willing to take a look at your progress and give help if you have any specific questions. Just reply here with your questions and updated project and I'll give feedback.


Check out my best projects so far and please comment:

Tic-Tac-Toe 2.0 ................... http://www.alice.org/community/showthread.php?t=3548
Alice Paint ........................... http://www.alice.org/community/showthread.php?p=23408
Real-Time Clock .................. http://www.alice.org/community/showthread.php?p=12725
Maze Game ......................... http://www.alice.org/community/showthread.php?t=4301
   
Reply With Quote