Commit e5af9e87 authored by Anna Maria Eilertsen's avatar Anna Maria Eilertsen
Browse files

added simple AI

parent 3d94d079
package inf101.v20.sem2.mnkgames.AI;
import inf101.v20.sem2.mnkgames.MNKGame;
public class SimpleStatelessAIPlayer {
public SimpleStatelessAIPlayer() {
}
public void makeMove(MNKGame game) {
for (int i = 0; i < game.getWidth(); i++) {
for(int j = 0; j < game.getHeight(); j++) {
try{
game.putPieceAt(i, j);
return;
}catch(IllegalArgumentException e) {
}
}
}
}
}
......@@ -20,6 +20,7 @@ import javax.swing.JPanel;
import inf101.v20.lab4.grid.Grid;
import inf101.v20.sem2.mnkgames.MNKGame;
import inf101.v20.sem2.mnkgames.MNKGame.Piece;
import inf101.v20.sem2.mnkgames.AI.SimpleStatelessAIPlayer;
/**
*
......@@ -102,10 +103,12 @@ s * Initializes a JFrame in which we place the game
private String player1_screenName;
private String player2_screenName;
private boolean multiplayer;
private SimpleStatelessAIPlayer ai;
public MNKGameGUI(GameSupplier gameSupplier) {
this.gameSupplier = gameSupplier;
this.game = gameSupplier.getTicTacToeGame();
this.ai = new SimpleStatelessAIPlayer();
}
/*
......@@ -133,7 +136,14 @@ s * Initializes a JFrame in which we place the game
this.updateUI();
}
private void updateBoardUI() {
private void registerClick() {
updateGameUI();
if(!multiplayer) {
AIPlayerTurn();
}
}
public void updateGameUI() {
clickablePanels.forEach(item -> item.updateToGame());
updateMessage();
MNKGameGUI.super.updateUI();
......@@ -144,6 +154,11 @@ s * Initializes a JFrame in which we place the game
}
private void AIPlayerTurn() {
ai.makeMove(game);
updateGameUI();
}
private void stop() {
updateMessage();
clickablePanels.forEach(item -> item.removeMouseListeners());
......@@ -233,7 +248,7 @@ s * Initializes a JFrame in which we place the game
public void click() {
game.putPieceAt(x, y);
updateBoardUI();
registerClick();
}
@Override
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment