-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCodeRunner.java
More file actions
75 lines (68 loc) · 2.22 KB
/
Copy pathCodeRunner.java
File metadata and controls
75 lines (68 loc) · 2.22 KB
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
70
71
72
73
74
75
import java.util.Scanner;
public class CodeRunner {
public int TILE_MULTIPLIER;
public int POWER_SPAWN_VALUE;
public int moves = 0;
public boolean runGame;
public Board board;
public void setRunGameValues() {
Scanner console = new Scanner(System.in);
moves = 0;
System.out.print("Multiplier: ");
TILE_MULTIPLIER = console.nextInt();
System.out.print("Power Spawn Value: ");
POWER_SPAWN_VALUE = console.nextInt();
System.out.print("Board Size: ");
board = new Board(console.nextInt());
}
//startGame() is run at the beginning of the game and randomly generates two tiles on the board
// based on the size of the board by calling createRandomTile() in a for() loop
public void startGame() {
setRunGameValues();
this.runGame = true;
for(int i = 0; i < board.getSize()/2; i++) {
createRandomTile();
}
System.out.println(board);
}
public void excecuteTurn(int direction) {
if(board.canMove()) {
board.moveContents(direction);
if(board.testTileMovement()) {
createRandomTile();
moves++;
}
System.out.println(board + "\n" + moves);
} else {
runGame = false;
}
}
//createRandomTile() randomly generates a tile in one of the open spaces on the board
public void createRandomTile() {
int x = (int)(Math.random() * board.getSize());
int y = (int)(Math.random() * board.getSize());
//problems happen when the board is full, it can't find an empty tile!!!
if(board.isFilled())
return;
while(board.getSpace(x,y) != null) {
x = (int)(Math.random() * board.getSize());
y = (int)(Math.random() * board.getSize());
}
board.setSpace(new Tile(TILE_MULTIPLIER,POWER_SPAWN_VALUE,board,x,y),x,y);
}
//getMove() returns what direction the board will scan in and move all of the Tiles in; be it through a System.in scanner
// or a randomly generated number, AI, ect.
public int getMove(Scanner console) {
int direction = 0;
direction = console.nextInt();
return direction;
}
//cheatGame() is a fun method I added for testing the code, that fills the entire Board with Tiles
public void cheatGame() {
for(int i = 0; i < board.getSize(); i++) {
for(int j = 0; j < board.getSize(); j++) {
Tile current = new Tile(TILE_MULTIPLIER,POWER_SPAWN_VALUE,board,i,j);
}
}
}
}