Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by T27M · Jun 22, 2012 at 02:40 AM · c#scoring

Scoring system

I made a simple tic tac toe game out of a 3x3 grid of squares. The squares have a script that changes the texture on mouse down depending on whos turn it is. Each successful turn it changes to the other player.

I currently have two scripts one attached to an empty which basicly chooses a random player to start the game and manages some events. The other attached to the grid squares to change the texture.

What I'm having trouble with is a way to detrimine if a player has won or if its a tie. I was thinking of assigning a vaule to a naught of -1 or cross +1 and adding the values of each row, column and diagonally but im having a hard time putting it into code.

Im probably overthinking it but some input would be appreciated.

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image torrente · Jun 22, 2012 at 03:10 AM 0
Share

It's not a full answer, but what I found to be the best logic to quickly checking for a winner without a huge mess of ifs or loops and such.

Try something like this:

//initialize each element of the array Box to 0

//set up a player number system to iterate between 1 and 2

//take turns, assigning the current player number to an element in an array

int winner = Box[0] (Box[0] == Box[1] && Box[0] == Box[2]) + Box[0] (Box[0] == Box[3] && Box[0] == Box[6]) + Box[0] (Box[0] == Box[4] && Box[0] == Box[8]) + Box[1] (Box[1] == Box[4] && Box[1] == Box[7]) + Box[2] (Box[2] == Box[5] && Box[2] == Box[8]) + Box[2] (Box[2] == Box[4] && Box[2] == Box[6]) + Box[3] (Box[3] == Box[4] && Box[3] == Box[5]) + Box[6] (Box[6] == Box[7] && Box[6] == Box[8]);

if (winner == 1) { //player1 wins, game ends or goes to play again and such }

if (winner == 2) { //player2 wins, game ends or goes to play again and such }

The block looks odd, but what it does is evaluate to either 0, 1, 2 depending on the player. it's a bit mathy but works out.

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Berenger · Jun 22, 2012 at 03:02 AM

I recently did a tic tac toe and to check if there was a victory, I did like this :

  • First, store the nine positions in an array (top left, top center, etc)

  • Second, there is 8 ways to win (3 rows, 3 columns, 2 diag). You can either create a class that hold 3 ints and then create an array of 8 of those, or an array of 8 * 3 int. Fill it with the correct indices.

  • Each time someone has played, check all those 8 possibilities to see if they contains 3 times the same thing.

I suggest you use an enum to know the status of each squares (Circle, Cross, Empty for instance).

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

Answer by Jessiechen · Jun 22, 2012 at 10:23 AM

I'd like to know how to ask questions? On this website, thank you

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image hirenkacha · Jun 22, 2012 at 10:28 AM 0
Share

on top, there is a link as "Ask a Question". Click on that and then ask your question with proper title and tag. Thats it..

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Making a bubble level (not a game but work tool) 1 Answer

An OS design issue: File types associated with their appropriate programs 1 Answer

Renderer on object disabled after level reload 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges