-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTileGame.java
69 lines (64 loc) · 2.05 KB
/
TileGame.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
//Original 2048 created by Gabriele Cirulli
//This version was created by Bradley Johnson
import java.util.Scanner;
//Create a new game
public class TileGame
{
public static void main(String[] args)
{
String input="";
Scanner scan = new Scanner(System.in);
Board board = new Board(4);
boolean winner=false;
System.out.println(board);
//loops until user wants to exit
while(!(input.equalsIgnoreCase("exit")))
{
//Prints the valid user inputs
System.out.println("RIGHT|LEFT|UP|DOWN|EXIT");
input=scan.next();
//Checks for right input and moves right if possible
if(input.equalsIgnoreCase("right")||input.equalsIgnoreCase("r"))
{
if(board.moveRight())
System.out.println(board+"\n");
else
System.out.println("Invalid Move.");
}
//Checks for left input and moves left if valid
if(input.equalsIgnoreCase("left")||input.equalsIgnoreCase("l"))
{
if(board.moveLeft())
System.out.println(board+"\n");
else
System.out.println("Invalid Move.");
}
//Checks for up movement and moves up if valid
if(input.equalsIgnoreCase("up")||input.equalsIgnoreCase("u"))
{
if(board.moveUp())
System.out.println(board+"\n");
else
System.out.println("Invalid Move.");
}
//Checks for downward move
if(input.equalsIgnoreCase("down")||input.equalsIgnoreCase("d"))
{
if(board.moveDown())
System.out.println(board+"\n");
else
System.out.println("Invalid Move.");
}
if((winner==false) && board.hasWon())
{
System.out.println("===================================\nYou have won! Type \"exit\" to quit.\n===================================");
winner=true;
}
if(!board.canMove())
{
System.out.println("You lost with a score of "+board.getScore()+".");
input="exit";
}
}
}
}