## tic tac toe game logic

I don't understand the game *Tic Tac Toe*. I've just started learning Android and followed a course that starts very easy, but now I am stuck at this game. Please, help me.

I am stuck at this code. Can any one explain me what is going on there?

for (int[] columnWinner : winner) { if (playerChoices[columnWinner[0]] == playerChoices[columnWinner[1]] && playerChoices[columnWinner[1]] == playerChoices[columnWinner[2]] && playerChoices[columnWinner[0]] != Player.NO) { Toast.makeText(getApplicationContext(), "We have Winner", Toast.LENGTH_LONG).show(); } }

This is the full code and it worked perfectly, without any errors.

Player currentPlayer = Player.ONE; Player[] playerChoices = new Player[9]; int[][] winner = {{0, 1, 2}, {3, 4, 5}, {6, 7, 8}, {0, 3, 6}, {1, 4, 7}, {2, 5, 8}, {0, 4, 8}, {2, 4, 6}}; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); playerChoices[0] = Player.NO; playerChoices[1] = Player.NO; playerChoices[2] = Player.NO; playerChoices[3] = Player.NO; playerChoices[4] = Player.NO; playerChoices[5] = Player.NO; playerChoices[6] = Player.NO; playerChoices[7] = Player.NO; playerChoices[8] = Player.NO; } public void imageViewIsTapped(View imageView) { ImageView tappedImage = (ImageView) imageView; tappedImage.setTranslationX(-2000); int tiTag = Integer.parseInt(tappedImage.getTag().toString()); playerChoices[tiTag] = currentPlayer; if (currentPlayer == Player.ONE) { tappedImage.setImageResource(R.drawable.tiger); currentPlayer = Player.TWO; } else if (currentPlayer == Player.TWO) { tappedImage.setImageResource(R.drawable.lion); currentPlayer = Player.ONE; } for (int[] columnWinner : winner) { if (playerChoices[columnWinner[0]] == playerChoices[columnWinner[1]] && playerChoices[columnWinner[1]] == playerChoices[columnWinner[2]] && playerChoices[columnWinner[0]] != Player.NO) { Toast.makeText(getApplicationContext(), "We have Winner", Toast.LENGTH_LONG).show(); } } tappedImage.animate().translationXBy(2000).alpha(1).rotation(3600).setDuration(1000); } enum Player { ONE, TWO, NO } }

int[][] winner = {{0, 1, 2}, {3, 4, 5}, {6, 7, 8}, {0, 3, 6}, {1, 4, 7}, {2, 5, 8}, {0, 4, 8}, {2, 4, 6}};

This is all possible cases when there is a winner. The first 3 is horizontal, the next 3 is vertical, the last 2 is diagonal, where the numbers are defined like this, indicated in the previous code:

0 | 1 | 2 ---+---+--- 3 | 4 | 5 ---+---+--- 6 | 7 | 8

Then let's analyse the core code:

for (int[] columnWinner : winner) { // traverses all cases if ( // if there is a case satisfied // for a specified case for example {0, 1, 2} playerChoices[columnWinner[0]] == playerChoices[columnWinner[1]] && // check if choice at 0 is the same as choice at 1 playerChoices[columnWinner[1]] == playerChoices[columnWinner[2]] && // check if choice at 1 is the same as choice at 2 // then choice at 0 1 2 are the same playerChoices[columnWinner[0]] != Player.NO // and this "the same" is not "they are all empty" ) { // then there is a winner

**Minimax Algorithm in Game Theory,** Let us combine what we have learnt so far about minimax and evaluation function to write a proper Tic-Tac-Toe AI (Artificial Intelligence) that plays a perfect game. Tic Tac Toe is probably the most popular board game in the world. I will describe the Tic Tac Toe logic and WINNING strategy. Tic Tac Toe rules briefly: Tic Tac Toe is a turn-based board game for two players, X and O. Tic Tac Toe game board is a 3x3 grid,

I am assuming you are asking about the for each loop:

for (int[] columnWinner : winner) {

The loop is called a for each loop that creates a variable and gives it a value for every iteration in the loop.

In this case, the loop creates an array of length 3 named columnWinner for each possible row, column, and diagonal on the tic-tac-toe board.

Each time through the loop, it checks whether the person has won be verifying if all three elements in the columnWinner array are the same:

if (playerChoices[columnWinner[0]] == playerChoices[columnWinner[1]] && playerChoices[columnWinner[1]] == playerChoices[columnWinner[2]]

And then checking to make sure they are filled in, instead of empty.

&& playerChoices[columnWinner[0]] != Player.NO) {

**Tic-tac-toe,** In today's article, I am going to show you how to create an unbeatable AI agent that plays the classic Tic Tac Toe game. Play Tic Tac Toe at MathPlayground.com! Enjoy this classic game of strategy and logic with a friend or play against the computer.

The 3 x 3 board is represented (rather strangely) by a one-dimensional array. All winning positions were determined by hand and listed in the `winner`

array, in triples because it takes 3 markers to occupy a winnng position in tic-tac-toe.

The loop you indicate is checked each of those winning positions in turn.

For example, consider winning position `{1, 4, 7}`

. The if-statement is checking whether the values at board positions 1, 4, 7 are identical and not equal to the 'NO' value which says no-one has played there.

The actual `winners`

data is structured as an array of (3-element) arrays; so the for-loop takes each 3-element array at a time and uses it to drive the 'if' statement. For example, when `columnWinner`

is `{1, 4, 7}`

then `columnWinner[0]`

is 4, and thus `playerChoices[columnWinner[0]]`

is looking at `playerChoices[4]`

.

**Tic Tac Toe,** About a week ago, I was just messing around on the internet and got a thought of creating Tic-tac-toe. step 1: Think about what logic you're gonna Implement. step � Classic Tic-Tac-Logic Play new puzzles each week. Each puzzle consists of a grid containing X's and O's in various places. The object is to place X or O in remaining squares so: There are no more than two consecutive X's or O's in a row or a column. The number of X's is the same as the number of O’s in each row and column.

**How can one program a tic-tac-toe game?,** This code makes a version of Tic Tac Toe with more features: *Move Logic This is a fortran95 implementation of the game of tic-tac-toe. Tic-Tac-Logic is a single player puzzle based on tic-tac-toe. Each puzzle consists of a grid containing X's and O's in various places. The object is to place X or O in the remaining squares so that: There are no more than two consecutive X's or O's in a row or column; the number of X's is the same as the number of O's in each row and column; and

**Tic-tac-toe,** Write a program that plays tic-tac-toe. The tic-tac-toe game is played on a 3x3 grid the Duration: 0:43
Posted: Jan 7, 2016 Tic-Tac-Toe in Nextion Logic This thread will show how to create a Tic-Tac-Toe within NEXTION Logic without the need for an external microprocessor. As we go through the process of creating this little game, I will deal with a few techniques that may be useful in your projects. So this is a relatively simple game, so let’s get started.

**Tic Tac toe Game Program in C,** This chapter features a Tic Tac Toe game against a simple artificial intelligence. An artificial intelligence (or AI) is a computer program that can intelligently� Coming to the TIC-TAC-TOE GAME. This project is a great exercise for beginners to get a hang of basic logic used in games and other applications. The logic behind the computer's moves is simpler than you think. To begin with, lets see the logical flow of code before getting into the 'code'.

##### Comments

- Hi Syed! welcome to Stack Overflow. I cant actually see that you've asked a question - tell us what you have tried and how you've attempted to fix it. If you are getting an error, post the logcat and error code so we can try to help :)
- i have not any error in my code i am just want explanation how the code flow is working in for loop which i mentioned
- i have a problem in this statement can you please explain in detail if (playerChoices[columnWinner[0]] == playerChoices[columnWinner[1]] && playerChoices[columnWinner[1]] == playerChoices[columnWinner[2]]
- Thank you sir now i am understood, Thank you \
- What is three elements
- if (playerChoices[columnWinner[0]] == playerChoices[columnWinner[1]] && playerChoices[columnWinner[1]] == playerChoices[columnWinner[2]]
- i did not understand about this condition
- please explain this condition if (playerChoices[columnWinner[0]] == playerChoices[columnWinner[1]] && playerChoices[columnWinner[1]] == playerChoices[columnWinner[2]