0% found this document useful (0 votes)
13 views2 pages

Tic Tac Toe Methods

Uploaded by

eeshu.soma
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views2 pages

Tic Tac Toe Methods

Uploaded by

eeshu.soma
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

//Eeshita Soma

//Computer Science II K - 4th

import java.util.Arrays;

public class TicTacToeMethods


{
public static void printGrid(String[][] grid)
{
for(String[] row : grid)
{
for(int i = 0; i < row.length; i++)
{
System.out.print(row[i]);
if( i < row.length - 1)
{
System.out.print(" | ");
}
}
System.out.println();
}
System.out.println();
}

public static void setGrid(String[][] grid)


{
for(int i = 0; i < grid.length; i++)
{
for(int j = 0; j < grid[i].length; j++)
{
grid[i][j] = "*";
}
}
}

public static boolean checkValid(String[][] grid, int row, int col)


{
return row >= 0 && row < grid.length && col >= 0 && col <
grid[0].length && grid[row][col].equals("*");
}

public static boolean checkWin(String[][] grid)


{
for(int i = 0; i < 3; i++)
{
if(grid[i][0].equals(grid[i][1]) && grid[i][1].equals(grid[i][2])
&& !grid[i][0].equals("*"))
{
return true;
}
if(grid[0][i].equals(grid[1][i]) && grid[1][i].equals(grid[2][i])
&& grid[0][i].equals("*"))
{
return true;
}
}

if(grid[0][0].equals(grid[1][1]) && grid[1][1].equals(grid[2][2]) && !


grid[0][0].equals("*"))
{
return true;
}
if(grid[0][2].equals(grid[1][1]) && grid[1][1].equals(grid[2][0]) && !
grid[0][2].equals("*"))
{
return true;
}
return false;
}

public static String whoWon(String[][] grid)


{
if(checkWin(grid))
{
for(int i = 0; i < 3; i++)
{
if(grid[i][0].equals(grid[i][1]) && grid[i]
[1].equals(grid[i][2]) && !grid[i][0].equals("*"))
{
return grid[i][0];
}
if(grid[0][i].equals(grid[1][i]) && grid[1]
[i].equals(grid[2][i]) && !grid[0][i].equals("*"))
{
return grid[0][i];
}
}
if(grid[0][0].equals(grid[1][1]) && grid[1][1].equals(grid[2]
[2]))
{
return grid[0][0];
}
if(grid[0][2].equals(grid[1][1]) && grid[1][1].equals(grid[2]
[0]))
{
return grid[0][2];
}
}
return "Draw";
}

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy