Last active
October 30, 2016 18:52
-
-
Save bhnascar/c2b1b8d5bcda66eb71ea7869a1138dfb to your computer and use it in GitHub Desktop.
TicTacToe: 2D array practice
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.*; | |
public class Solution | |
{ | |
public static class TicTacToe | |
{ | |
/* | |
* This board will represent all the plays made in | |
* the game so far. | |
* | |
* We will use the following representation: | |
* A 0 at any position means no player has played | |
* there yet. A 1 at any position means player 1 has | |
* played there. A 2 at any position means player 2 | |
* has played there. | |
* | |
* This is what a 3 by 3 board might look like after | |
* three plays by player 1 and two plays by player 2: | |
* | |
* [[1, 0, 0], | |
* [0, 1, 2], | |
* [1, 0, 2]] | |
*/ | |
int[][] board; | |
/* | |
* Notice the |board| variable is not yet initialized. | |
* Use the given |width| and |height| here to initialize | |
* the |board| so that it is a 2D array with the proper | |
* width and height. | |
* | |
* So if |width| is 3 and |height| is 3, |board| should | |
* become a 3x3 2D array. If |width| is 5 and |height| is 6 | |
* board should become a 6x5 2D array (think: why is it | |
* 6x5 and not 5x6?). | |
*/ | |
public TicTacToe(int width, int height) { | |
// TODO: implement | |
// board = new...?? | |
} | |
/* | |
* This method should insert a move at a the given | |
* position in the board, made by the player given | |
* by the variable |player|. | |
* | |
* For example, if player 1 makes a move at position | |
* (1, 2), then then this method will be called | |
* with the variable |player| set to 1, x set to 1 | |
* and y set to 2. This method should update the | |
* |board| variable accordingly. | |
*/ | |
public void play(int player, int x, int y) { | |
// TODO: implement | |
} | |
/* This method should return true if the |board| | |
* is completely full of moves (i.e. there are | |
* no more plays either player can make). */ | |
public boolean isBoardFull() { | |
// TODO: implement | |
return false; | |
} | |
/* This method should return true if there is a | |
* three-in-a-row anywhere on |board|. */ | |
public boolean isOver() { | |
// TODO: implement | |
return false; | |
} | |
/* Parses a user move, given by |move|. It will be a | |
* String of the format "(x, y)" where x is the | |
* x-location on the board, and y is the y-location | |
* on the board. | |
* | |
* This method should return a 2D int array that indicates | |
* the move. The x-location should be at the zero index | |
* and the y-location should be at the 1 index. | |
*/ | |
public int[] parseMove(String move) { | |
// TODO: implement | |
return null; | |
} | |
/* This method should return a String representation | |
* of the game. It should create a new line for each row | |
* of the |board|. | |
* | |
* So if the game board was the following: | |
* [[1, 0, 0], | |
* [0, 1, 2], | |
* [1, 0, 2]] | |
* | |
* This method should return the String: | |
* "1 0 0\n0 1 2\n 1 0 2" | |
* | |
* where "\n" is the new line character. | |
*/ | |
public String toString() { | |
// TOOD: implement | |
return null; | |
} | |
} | |
public static void main(String[] args) { | |
Scanner input = new Scanner(System.in); | |
TicTacToe game = new TicTacToe(3, 3); | |
System.out.println("Starting a 3x3 game of TicTacToe!"); | |
while(true) { | |
// Tell player 1 to make a move. | |
System.out.println("Player 1, make a move! " | |
+ "Type a location in the format (x,y)."); | |
// Get player 1's move. | |
int[] p1Move = game.parseMove(input.nextLine()); | |
// Add it to the game board. | |
game.play(1, p1Move[0], p1Move[1]); | |
// Display the game board. | |
System.out.println(game); | |
// Check if the game has ended. | |
if (game.isOver()) { | |
System.out.println("Player 1 wins!"); | |
break; | |
} | |
// Repeating the same thing for player 2. | |
System.out.println("Player 2, make a move! " | |
+ "Type a location in the format (x,y)."); | |
int[] p2Move = game.parseMove(input.nextLine()); | |
game.play(2, p2Move[0], p2Move[1]); | |
System.out.println(game); | |
if (game.isOver()) { | |
System.out.println("Player 2 wins!"); | |
break; | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment