gdx-studio
git clone https://git.pyrossh.dev/gdx-studio
An IDE for creating Games using libgdx and Java supported on all platforms Android, iOS, Desktop
test/source/TicTacToe.java
| a9b2a64 | 1 | package source; |
| a9b2a64 | 2 | |
| a62d533 | 3 | import com.badlogic.gdx.scenes.scene2d.Actor; |
| a62d533 | 4 | import com.badlogic.gdx.scenes.scene2d.Group; |
| a62d533 | 5 | import com.badlogic.gdx.scenes.scene2d.ui.Image; |
| a62d533 | 6 | import com.badlogic.gdx.scenes.scene2d.InputEvent; |
| a62d533 | 7 | import com.badlogic.gdx.scenes.scene2d.utils.ClickListener; |
| a62d533 | 8 | |
| a62d533 | 9 | import java.util.Random; |
| a62d533 | 10 | |
| a9b2a64 | 11 | import scene2d.*; |
| a9b2a64 | 12 | |
| a62d533 | 13 | /** |
| 7c84877 | 14 | * A Basic TicTacToe Game |
| 7c84877 | 15 | Scene |
| a62d533 | 16 | * |
| a62d533 | 17 | * @author pyros2097 */ |
| a62d533 | 18 | public class TicTacToe extends Scene {
|
| a62d533 | 19 | |
| a62d533 | 20 | Box[][] boxes; |
| a62d533 | 21 | public static int turnCounter = 0; |
| a62d533 | 22 | |
| a62d533 | 23 | /* Game Constants */ |
| a62d533 | 24 | private static GameMode gameMode = GameMode.SINGLE_PLAYER_VS_COMPUTER; |
| a62d533 | 25 | public static Turn currentTurn = Turn.Player; |
| a62d533 | 26 | |
| a62d533 | 27 | public TicTacToe(){
|
| a62d533 | 28 | boxes = new Box[3][3]; |
| a62d533 | 29 | for(int i = 0;i < 3;i++){
|
| a62d533 | 30 | for(int j = 0;j < 3;j++){
|
| a62d533 | 31 | Box box = new Box(i, j); |
| a62d533 | 32 | boxes[i][j] = box; |
| a9b2a64 | 33 | addActor(box, box.getWidth()*j, box.getHeight()*i); |
| a62d533 | 34 | } |
| a62d533 | 35 | } |
| a62d533 | 36 | } |
| a62d533 | 37 | |
| a62d533 | 38 | /* Check if mode is current return true */ |
| a62d533 | 39 | public static boolean mode(GameMode gm){
|
| a62d533 | 40 | if(gameMode == gm) |
| a62d533 | 41 | return true; |
| a62d533 | 42 | else |
| a62d533 | 43 | return false; |
| a62d533 | 44 | } |
| a62d533 | 45 | |
| a62d533 | 46 | public static void setMode(GameMode gm){
|
| a62d533 | 47 | gameMode = gm; |
| a9b2a64 | 48 | log("Game Mode: " + gameMode.toString());
|
| a62d533 | 49 | } |
| a62d533 | 50 | |
| a62d533 | 51 | public void reset(){
|
| a9b2a64 | 52 | log("Reset");
|
| a62d533 | 53 | turnCounter = 0; |
| a9b2a64 | 54 | setScene("TicTacToe");
|
| a62d533 | 55 | } |
| a62d533 | 56 | |
| a62d533 | 57 | public void playerWin(){
|
| a9b2a64 | 58 | log("Player Win");
|
| a62d533 | 59 | reset(); |
| a62d533 | 60 | } |
| a62d533 | 61 | |
| a62d533 | 62 | public void computerWin(){
|
| a9b2a64 | 63 | log("Computer Win");
|
| a62d533 | 64 | reset(); |
| a62d533 | 65 | } |
| a62d533 | 66 | |
| a62d533 | 67 | int random1 = 0; |
| a62d533 | 68 | int random2 = 0; |
| a62d533 | 69 | Random rand = new Random(); |
| a62d533 | 70 | void AI(){
|
| a62d533 | 71 | if(mode(GameMode.SINGLE_PLAYER_VS_COMPUTER)){
|
| a62d533 | 72 | if(currentTurn == Turn.Computer){
|
| a9b2a64 | 73 | log("AI");
|
| a62d533 | 74 | random1 = rand.nextInt(3); |
| a62d533 | 75 | random2 = rand.nextInt(3); |
| a62d533 | 76 | if(!boxes[random1][random2].isMarked){
|
| a62d533 | 77 | boxes[random1][random2].markByComputer(); |
| a62d533 | 78 | return; |
| a62d533 | 79 | } |
| a62d533 | 80 | } |
| a62d533 | 81 | } |
| a62d533 | 82 | } |
| a62d533 | 83 | |
| a62d533 | 84 | void checkForRowY(int j){
|
| a62d533 | 85 | if(boxes[j][0].type != MarkType.None && boxes[j][1].type != MarkType.None |
| a62d533 | 86 | && boxes[j][2].type != MarkType.None ) |
| a62d533 | 87 | if(boxes[j][0].type == boxes[j][1].type && boxes[j][0].type == boxes[j][2].type ) |
| a62d533 | 88 | if(boxes[j][0].type == MarkType.X) |
| a62d533 | 89 | playerWin(); |
| a62d533 | 90 | else |
| a62d533 | 91 | computerWin(); |
| a62d533 | 92 | } |
| a62d533 | 93 | |
| a62d533 | 94 | void checkForRowX(int j){
|
| a62d533 | 95 | if(boxes[0][j].type != MarkType.None && boxes[1][j].type != MarkType.None |
| a62d533 | 96 | && boxes[2][j].type != MarkType.None ) |
| a62d533 | 97 | if(boxes[0][j].type == boxes[1][j].type && boxes[0][j].type == boxes[2][j].type ) |
| a62d533 | 98 | if(boxes[0][j].type == MarkType.X) |
| a62d533 | 99 | playerWin(); |
| a62d533 | 100 | else |
| a62d533 | 101 | computerWin(); |
| a62d533 | 102 | } |
| a62d533 | 103 | |
| a62d533 | 104 | void checkForDiagonal(){
|
| a62d533 | 105 | if(boxes[0][0].type != MarkType.None && boxes[1][1].type != MarkType.None |
| a62d533 | 106 | && boxes[2][2].type != MarkType.None ) |
| a62d533 | 107 | if(boxes[0][0].type == boxes[1][1].type && boxes[0][0].type == boxes[2][2].type ) |
| a62d533 | 108 | if(boxes[0][0].type == MarkType.X) |
| a62d533 | 109 | playerWin(); |
| a62d533 | 110 | else |
| a62d533 | 111 | computerWin(); |
| a62d533 | 112 | if(boxes[0][2].type != MarkType.None && boxes[1][1].type != MarkType.None |
| a62d533 | 113 | && boxes[2][0].type != MarkType.None ) |
| a62d533 | 114 | if(boxes[0][2].type == boxes[1][1].type && boxes[0][2].type == boxes[2][0].type ) |
| a62d533 | 115 | if(boxes[0][2].type == MarkType.X) |
| a62d533 | 116 | playerWin(); |
| a62d533 | 117 | else |
| a62d533 | 118 | computerWin(); |
| a62d533 | 119 | } |
| a62d533 | 120 | |
| a62d533 | 121 | @Override |
| a62d533 | 122 | public void act(float delta){
|
| a62d533 | 123 | super.act(delta); |
| a62d533 | 124 | AI(); |
| a62d533 | 125 | if(turnCounter < 9){
|
| a62d533 | 126 | for(int i=0; i<3;i++){
|
| a62d533 | 127 | checkForRowY(i); |
| a62d533 | 128 | checkForRowX(i); |
| a62d533 | 129 | } |
| a62d533 | 130 | checkForDiagonal(); |
| a62d533 | 131 | } |
| a62d533 | 132 | else{
|
| a9b2a64 | 133 | log("Draw Match");
|
| a62d533 | 134 | reset(); |
| a62d533 | 135 | } |
| a62d533 | 136 | } |
| a62d533 | 137 | |
| a62d533 | 138 | @Override |
| a62d533 | 139 | public void onClick(Actor actor){}
|
| a62d533 | 140 | |
| a62d533 | 141 | @Override |
| a62d533 | 142 | public void onTouchDown(Actor actor){}
|
| a62d533 | 143 | |
| a62d533 | 144 | @Override |
| a62d533 | 145 | public void onTouchUp(){}
|
| a62d533 | 146 | |
| a62d533 | 147 | @Override |
| a62d533 | 148 | public void onDragged(){}
|
| a62d533 | 149 | |
| a62d533 | 150 | @Override |
| a62d533 | 151 | public void onGesture(GestureType type){}
|
| a9b2a64 | 152 | |
| a9b2a64 | 153 | @Override |
| a9b2a64 | 154 | public void onKeyTyped(char key){
|
| a9b2a64 | 155 | } |
| a9b2a64 | 156 | |
| a9b2a64 | 157 | @Override |
| a9b2a64 | 158 | public void onKeyUp(int keycode){
|
| a9b2a64 | 159 | } |
| a9b2a64 | 160 | |
| a9b2a64 | 161 | @Override |
| a9b2a64 | 162 | public void onKeyDown(int keycode){
|
| a9b2a64 | 163 | } |
| a62d533 | 164 | |
| a62d533 | 165 | @Override |
| a62d533 | 166 | public void onPause(){
|
| a62d533 | 167 | } |
| a62d533 | 168 | |
| a62d533 | 169 | @Override |
| a62d533 | 170 | public void onResume(){
|
| a62d533 | 171 | } |
| a62d533 | 172 | |
| a62d533 | 173 | @Override |
| a62d533 | 174 | public void onDispose(){
|
| a62d533 | 175 | } |
| a62d533 | 176 | } |
| a62d533 | 177 | |
| a62d533 | 178 | class Box extends Group{
|
| a62d533 | 179 | private final int row, col; |
| a62d533 | 180 | private final Image bg = new Image(Asset.tex("square"));
|
| a62d533 | 181 | private final Image x = new Image(Asset.tex("x"));
|
| a62d533 | 182 | private final Image o = new Image(Asset.tex("o"));
|
| a62d533 | 183 | public boolean isMarked = false; |
| a62d533 | 184 | public MarkType type = MarkType.None; |
| a62d533 | 185 | |
| a62d533 | 186 | Box(int row, int col){
|
| a62d533 | 187 | this.row = row; |
| a62d533 | 188 | this.col = col; |
| a62d533 | 189 | bg.setPosition(row*bg.getWidth(), col*bg.getHeight()); |
| a62d533 | 190 | o.setPosition(row*bg.getWidth(), col*bg.getHeight()); |
| a62d533 | 191 | x.setPosition(row*bg.getWidth(), col*bg.getHeight()); |
| a62d533 | 192 | addActor(bg); |
| a62d533 | 193 | addListener(new ClickListener(){
|
| a62d533 | 194 | @Override |
| a62d533 | 195 | public void clicked(InputEvent event, float x, float y) {
|
| a62d533 | 196 | super.clicked(event, x, y); |
| a62d533 | 197 | if(!Box.this.isMarked){
|
| a62d533 | 198 | if(TicTacToe.mode(GameMode.SINGLE_PLAYER)) |
| a62d533 | 199 | if(TicTacToe.currentTurn == Turn.Player) |
| a62d533 | 200 | markByPlayer(); |
| a62d533 | 201 | else |
| a62d533 | 202 | markByComputer(); |
| a62d533 | 203 | else if(TicTacToe.mode(GameMode.SINGLE_PLAYER_VS_COMPUTER)) |
| a62d533 | 204 | if(TicTacToe.currentTurn == Turn.Player) |
| a62d533 | 205 | markByPlayer(); |
| a9b2a64 | 206 | Scene.log("Box Clicked "+Box.this.row+Box.this.col);
|
| a62d533 | 207 | } |
| a62d533 | 208 | } |
| a62d533 | 209 | }); |
| a62d533 | 210 | } |
| a62d533 | 211 | |
| a62d533 | 212 | public void markByPlayer(){
|
| a62d533 | 213 | isMarked = true; |
| a62d533 | 214 | TicTacToe.turnCounter++; |
| a62d533 | 215 | addActor(x); |
| a62d533 | 216 | TicTacToe.currentTurn = Turn.Computer; |
| a62d533 | 217 | type = MarkType.X; |
| a62d533 | 218 | } |
| a62d533 | 219 | |
| a62d533 | 220 | public void markByComputer(){
|
| a62d533 | 221 | isMarked = true; |
| a62d533 | 222 | TicTacToe.turnCounter++; |
| a62d533 | 223 | addActor(o); |
| a62d533 | 224 | TicTacToe.currentTurn = Turn.Player; |
| a62d533 | 225 | type = MarkType.O; |
| a62d533 | 226 | } |
| a62d533 | 227 | } |
| a62d533 | 228 | |
| a62d533 | 229 | enum MarkType {
|
| a62d533 | 230 | None, |
| a62d533 | 231 | X, |
| a62d533 | 232 | O |
| a62d533 | 233 | } |
| a62d533 | 234 | |
| a62d533 | 235 | enum Turn {
|
| a62d533 | 236 | Player, |
| a62d533 | 237 | Computer, |
| a62d533 | 238 | OtherPlayer |
| a62d533 | 239 | } |
| a62d533 | 240 | |
| a62d533 | 241 | enum GameMode {
|
| a62d533 | 242 | SINGLE_PLAYER, |
| a62d533 | 243 | SINGLE_PLAYER_VS_COMPUTER, |
| a62d533 | 244 | MULTI_PLAYER, |
| a9b2a64 | 245 | } |