You are on page 1of 19

return result;

package jgomoku;
public class GomokuGame extends BoardData{
private char winner;
private boolean newGame=true;
private GomokuGameHistory gameHistory;
private boolean isGameOver;
public GomokuGame(){
super();
gameHistory=new GomokuGameHistory();
winner='o';
isGameOver=false;
}
public GomokuGame(int size){
super(size);
gameHistory=new GomokuGameHistory();
winner='o';
isGameOver=false;
}
public GomokuGame(GomokuGameHistory ggh){
gameHistory=ggh;
isGameOver=true;
}
public GomokuGame(char[][] board){
super(15);
this.board=board;
}
@Override
public boolean moveWhite(int row , int column){
if(isGameOver){
return false;
}
boolean result=super.moveWhite(row , column);
if(result){
newGame=false;
gameHistory.addMove(row , column);
gameHistory.getNextMove();
checkWinner('w');
}

}
@Override
public boolean moveBlack(int row , int column){
if(isGameOver){
return false;
}
boolean result=super.moveBlack(row, column);
if(result){
newGame=false;
gameHistory.addMove(row , column);
gameHistory.getNextMove();
checkWinner('b');
}
return result;
}
public Move previousMove(){
return gameHistory.getPreviousMove();
}
public Move nextMove(){
return gameHistory.getNextMove();
}
public boolean saveGame(String file){
return gameHistory.saveGame(file);
}
public boolean checkWinner(char side){
boolean isOver=false;
if(side != 'w'){
if(checkHorizontalWinner('b')){
isOver=true;
}
if(!isOver && checkVerticalWinner('b')){
isOver=true;
}
if(!isOver && checkMainDiagonalWinner('b')){
isOver=true;
}
if(!isOver && checkSecondaryDiagonalWinner('b')){
isOver=true;

}
if(isOver){
winner='b';
isGameOver=true;
return true;
}
}
if(side != 'b'){
if(checkHorizontalWinner('w')){
isOver=true;
}
if(!isOver && checkVerticalWinner('w')){
isOver=true;
}
if(!isOver && checkMainDiagonalWinner('w')){
isOver=true;
}
if(!isOver && checkSecondaryDiagonalWinner('w')){
isOver=true;
}
if(isOver){
winner='w';
isGameOver=true;
return true;
}
}
return false;

}
length=0;
}
return false;
}
private boolean checkVerticalWinner(char side){
int row , column;
int length=0;
for(column=0 ; column<size ; column++){
for(row=0 ; row<size ; row++){
if(board[row][column] == side){
length++;
}
else{
length=0;
}
if(length == 5){
return true;
}
}
length=0;
}
return false;

private boolean checkHorizontalWinner(char side){


int row , column;
int length=0;

private boolean checkMainDiagonalWinner(char side){


int row , column;
int length=0;
int aux;

for(row=0 ; row<size ; row++){


for(column=0 ; column<size ; column++){
if(board[row][column] == side){
length++;
}
else{
length=0;
}
if(length == 5){
return true;
}

//diagonals above , parallel to and including the main


board matrice diagonal
for(aux=0 ; aux<size ; aux++){
for(column=size-1-aux , row=0 ; column<size ;
column++ , row++){
if(board[row][column] == side){
length++;
}
else{
length=0;

}
if(length == 5){
return true;
}

length=0;
}
if(length == 5){
return true;
}

}
length=0;

}
length=0;

}
}
//diagonals below and parallel to the main board
matrice diagonal
for(aux=0 ; aux<size-1 ; aux++){
for(row=size-1-aux , column=0 ; row<=size-1 ;
row++ , column++){
if(board[row][column] == side){
length++;
}
else{
length=0;
}
if(length == 5){
return true;
}
}
length=0;
}

//diagonals below and parallel to the secondary


board matrice diagonal
for(aux=0 ; aux<size-1 ; aux++){
for(column=size-1-aux , row=size-1 ; column<=size1 ; column++ , row--){
if(board[row][column] == side){
length++;
}
else{
length=0;
}
if(length == 5){
return true;
}
}
length=0;
}

return false;
}

return false;
}

private boolean checkSecondaryDiagonalWinner(char


side){
int row , column;
int length=0;
int aux;
//diagonals above , parallel to and including the
secondary board matrice diagonal
for(aux=0 ; aux<size ; aux++){
for(row=aux , column=0 ; row>=0 ; row-- ,
column++){
if(board[row][column] == side){
length++;
}
else{

public boolean isNewGame(){


return newGame;
}
public boolean isGameOver(){
return isGameOver;
}
public boolean isBlackWinner(){
if(winner == 'b'){
return true;
}
return false;
}

public char[][] exportPositionToAi(){


int row , column;
char[][] bd=new char[size][size];
for(row=0 ; row<size ; row++){
for(column=0 ; column<size ; column++){
bd[row][column]=board[row][column];
}
}
return bd;
}
//only used by gomokuAi
public boolean resetAndCheckBoardPosition(char[][]
boardPosition , char side){
this.board=boardPosition;
if(checkWinner(side)){
return true;
}
return false;
}
}

package jgomoku;

return m;
}

import java.io.*;
import java.util.*;

public Move getPreviousMove(){


Move m;

public class GomokuGameHistory {


private ArrayList moves;
private int currentMove;
private boolean gameFinished;

if(! gameFinished){
return null;
}
if(currentMove==0){
return null;
}
currentMove--;
m= (Move) moves.get(currentMove);
if(currentMove%2 == 0){
m.isBlack=true;
}
else{
m.isBlack=false;
}

GomokuGameHistory(){
moves=new ArrayList();
gameFinished=false;
currentMove=0;
}
public void addMove(int row , int column){
Move m=new Move(row , column);
moves.add(m);
currentMove++;
}

return m;
public void setGameFinished(){
gameFinished=true;
}
public Move getNextMove(){
Move m;
if(! gameFinished){
return null;
}
if(currentMove == moves.size()){
return null;
}
m= (Move) moves.get(currentMove);
if(currentMove%2 == 0){
m.isBlack=true;
}
else{
m.isBlack=false;
}
currentMove++;

}
public boolean loadGame(String filename){
try {
BufferedReader bfR = new BufferedReader(new
FileReader(filename));
while(true){
try {
String line =bfR.readLine();
if(line == null){
if(moves.size() > 0){
currentMove=0;
gameFinished=true;
return true;
}
return false;
}
int spaceIndex=line.indexOf(" ");
int n1=Integer.parseInt(line.subSequence(0 ,
spaceIndex).toString());

int
n2=Integer.parseInt(line.substring(spaceIndex+1).toString(
));
Move m=new Move(n1 , n2);
moves.add(m);
}
catch (IOException ex) {
ex.printStackTrace();
return false;
}
}

package jgomoku;
public class Move {
public int row;
public int column;
public boolean isBlack;
Move(){
}
Move(int row , int column){
this.row=row;
this.column=column;
}

}
catch (FileNotFoundException ex) {
ex.printStackTrace();
}

Move(int row , int column , boolean isBlack){


this.row=row;
this.column=column;
this.isBlack=isBlack;
}

return false;
}
public boolean saveGame(String file){
try {
BufferedWriter bfW = new BufferedWriter(new
FileWriter(file));
Iterator<Move> it=moves.iterator();
Move m=null;
while(it.hasNext()){
m=it.next();
bfW.write(m.row + " " + m.column + "\n");
}
bfW.flush();
bfW.close();
return true;
}
catch (IOException ex) {
ex.printStackTrace();
return false;
}
}
}

/*
*Copyright 2008 Cristian Achim
*
* Licensed under the Apache License, Version 2.0 (the
"License");
* you may not use this file except in compliance with the
License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in
writing,
* software distributed under the License is distributed on
an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
either express or implied.
* See the License for the specific language governing
permissions and
* limitations under the License.
*
*/
package jgomoku;
import java.util.*;
public class GameController/* extends TimerTask*/{
private GomokuGame gomokuGame;
private UserInterface humanUserInterface;
private boolean blackHuman , whiteHuman;
private GomokuGameHistory gameHistory;
private boolean waitMove;
private boolean waitBlack;
private boolean doingReplay;
private Thread t;
GameController(){
humanUserInterface=new
GraphicalInterfaceController(15 , 15);
humanUserInterface.setCallback(this);
waitMove=false;
doingReplay=false;
}

GameController(boolean graphical){
if(graphical == true){
humanUserInterface=new
GraphicalInterfaceController(15 , 15);
}
else{
humanUserInterface=new TextInterface();
}
humanUserInterface.setCallback(this);
waitMove=false;
doingReplay=false;
}
public void newGame(boolean blackHuman , boolean
whiteHuman){
gomokuGame=new GomokuGame(15);
if(blackHuman){
humanUserInterface.printText("waiting for black
move");
}
else{
humanUserInterface.printText("waiting for ai
move");
}
if(t != null){
if(t.isAlive()){
t.interrupt();
}
}
this.blackHuman=blackHuman;
this.whiteHuman=whiteHuman;
this.waitMove=true;
this.waitBlack=true;
if(! blackHuman){
t=new Thread(new
GomokuAi(gomokuGame.exportPositionToAi() , true , this
, 2));
t.start();
}
}
public boolean removeOldGame(){
if(this.waitMove || this.doingReplay){

this.waitMove=false;
this.doingReplay=false;

humanUserInterface.printText("black won the


game");
humanUserInterface.gameFinished(true , row ,

return true;
}
return false;
}
private void saveGame(String fileName){
if(this.gomokuGame.saveGame(fileName)){
this.humanUserInterface.printText("game saved");
}
else{
this.humanUserInterface.printText("game saving
failed");
}
}
private void loadGame(String fileName){
GomokuGameHistory ggh;
ggh=new GomokuGameHistory();
if(ggh.loadGame(fileName)){
gameHistory=ggh;
gomokuGame=new GomokuGame(gameHistory);
doingReplay=true;
humanUserInterface.setReplayMode();
}
else{
humanUserInterface.printText("game file loading
failed");
}
}
private void makeMove(int row , int column){
if(! this.waitMove){
System.out.println("error not waiting move");
return;
}
if(waitBlack && blackHuman){
if(gomokuGame.moveBlack(row, column)){
if(gomokuGame.isGameOver()){

column);
doingReplay=true;
waitMove=false;
waitBlack=false;
}
else if(whiteHuman){
humanUserInterface.printText("waiting for
white move");
humanUserInterface.getWhiteMove(row ,
column);
waitBlack=false;
}
else{
humanUserInterface.printText("waiting for ai
move");
humanUserInterface.waitAiMove(true , row ,
column);
t=new Thread(new
GomokuAi(gomokuGame.exportPositionToAi() , false , this
, 2));
waitBlack=false;
t.start();
}
//waitBlack=false;-moved into to if elses
}
else{
humanUserInterface.printText("illegal move");
}
}
else if(! waitBlack && whiteHuman){
if(gomokuGame.moveWhite(row, column)){
if(gomokuGame.isGameOver()){
humanUserInterface.printText("white won the
game");
humanUserInterface.gameFinished(false , row ,
column);
doingReplay=true;
waitMove=false;
waitBlack=true;
}
else if(blackHuman){

humanUserInterface.printText("waiting for
black move");
humanUserInterface.getBlackMove(row ,
column);
waitBlack=true;
}
else{
humanUserInterface.printText("waiting for ai
move");
humanUserInterface.waitAiMove(false , row ,
column);
t=new Thread(new
GomokuAi(gomokuGame.exportPositionToAi() , false , this
, 2));
waitBlack=true;
t.start();
}
//waitBlack=true;-moved into the if elses
}
else{
humanUserInterface.printText("illegal move");
}
}
else if(waitBlack && ! blackHuman){
if(gomokuGame.moveBlack(row, column)){
if(gomokuGame.isGameOver()){
humanUserInterface.printText("black won the
game");
humanUserInterface.gameFinished(true , row ,
column);
doingReplay=true;
waitMove=false;
waitBlack=false;
}
else if(whiteHuman){
humanUserInterface.printText("waiting for
white move");
humanUserInterface.getWhiteMove(row,
column);
waitBlack=false;
}
else{
humanUserInterface.printText("waiting for ai
move");

t=new Thread(new
GomokuAi(gomokuGame.exportPositionToAi() , false , this
, 2));
humanUserInterface.waitAiMove(true, row,
column);
waitBlack=false;
t.start();
}
//waitBlack=false;-moved into the if elses
}
else{
humanUserInterface.printText("illegal ai move");
}
}
else if(! waitBlack && ! whiteHuman){
if(gomokuGame.moveWhite(row, column)){
if(gomokuGame.isGameOver()){
humanUserInterface.printText("white won the
game");
humanUserInterface.gameFinished(false, row,
column);
doingReplay=true;
waitMove=false;
waitBlack=true;
}
else if(blackHuman){
humanUserInterface.printText("waiting for
black move");
humanUserInterface.getBlackMove(row,
column);
waitBlack=true;
}
else{
humanUserInterface.printText("waiting for ai
move");
t=new Thread(new
GomokuAi(gomokuGame.exportPositionToAi() , true , this
, 2));
humanUserInterface.waitAiMove(false, row,
column);
waitBlack=true;
t.start();
}
//waitBlack=true;-moved to the if elses

}
else{
humanUserInterface.printText("illegal ai move");
}
}
else{
humanUserInterface.printText("weird boolean
error");
}
}
private void previousMove(){
if(doingReplay){
Move m=gomokuGame.previousMove();
if(m == null){
humanUserInterface.printText("reached first
move");
}
else{
if(m.isBlack){
humanUserInterface.removeBlack(m.row,
m.column);
}
else{
humanUserInterface.removeWhite(m.row,
m.column);
}
}
}
else{
humanUserInterface.printText("error no game
replay");
}
}
private void nextMove(){
if(doingReplay){
Move m=gomokuGame.nextMove();
if(m == null){
humanUserInterface.printText("reached last
move");
}
else{
if(m.isBlack){

humanUserInterface.moveBlack(m.row,
m.column);
}
else{
humanUserInterface.moveWhite(m.row,
m.column);
}
}
}
else{
humanUserInterface.printText("error no game
replay");
}
}
//the callback function from gomokuai ,
graphicalinterface or textinterface
public void sendPlayerInput(String input){
String token;
StringTokenizer st=new StringTokenizer(input);
int value1 , value2;
boolean blackHuman , whiteHuman;
/*
* all posible input commands:
*
* move row column
* previous
* next
* new blackplayer whiteplayer ;
whiteplayer,blackplayer=human | computer
* load filelocationandnamestring
* save filelocationandnamestring
*/
if(! st.hasMoreTokens()){
return;
}
token=st.nextToken();
if(token.equals("move")){
if(! st.hasMoreTokens()){
return;
}
token=st.nextToken();

if(! st.hasMoreTokens()){
return;
}
token=st.nextToken();

try{
value1=Integer.parseInt(token);
}
catch(Exception e){
e.printStackTrace();
return;
}
if(! st.hasMoreTokens()){
return;
}
token=st.nextToken();

if(token.equals("human")){
whiteHuman=true;
}
else if(token.equals("computer")){
whiteHuman=false;
}
else{
return;
}

try{
value2=Integer.parseInt(token);
}
catch(Exception e){
e.printStackTrace();
return;
}

newGame(blackHuman , whiteHuman);
}
else if(token.equals("load")){
if(! st.hasMoreTokens()){
return;
}
token=st.nextToken();

makeMove(value1 , value2);
}
else if(token.equals("previous")){
previousMove();
}
else if(token.equals("next")){
nextMove();
}
else if(token.equals("new")){
if(! st.hasMoreTokens()){
return;
}
token=st.nextToken();

loadGame(token);
}
else if(token.equals("save")){
if(! st.hasMoreTokens()){
return;
}
token=st.nextToken();
saveGame(token);
}
}
}

if(token.equals("human")){
blackHuman=true;
}
else if(token.equals("computer")){
blackHuman=false;
}
else{
return;
}

package jgomoku;
import java.awt.image.*;
import java.io.*;
import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.RenderingHints;
import javax.swing.JFileChooser;
public class GraphicalInterface extends javax.swing.JPanel
{

imageReader=this.getClass().getResourceAsStream("/artw
ork/whitestone.png");
whiteStone=ImageIO.read(imageReader);
imageReader.close();

imageReader=this.getClass().getResourceAsStream("/artw
ork/middlenode.png");
middleNode=ImageIO.read(imageReader);
imageReader.close();

private BufferedImage boardImage;


private BufferedImage blackStone;
private BufferedImage whiteStone;
private BufferedImage middleNode;
private BufferedImage northNode;
private BufferedImage northEastNode;
private BufferedImage eastNode;
private BufferedImage southEastNode;
private BufferedImage southNode;
private BufferedImage southWestNode;
private BufferedImage westNode;
private BufferedImage northWestNode;

imageReader=this.getClass().getResourceAsStream("/artw
ork/northnode.png");
northNode=ImageIO.read(imageReader);
imageReader.close();

private static int MAXROW;


private static int MAXCOLUMN;

imageReader=this.getClass().getResourceAsStream("/artw
ork/eastnode.png");
eastNode=ImageIO.read(imageReader);
imageReader.close();

private int boardImageHeight;


private int boardImageWidth;
GraphicalInterfaceController gic;
private void imageInitialisation(){
InputStream imageReader;
//loading images from files in artwork folder in jar file
try{
imageReader=this.getClass().getResourceAsStream("/artw
ork/blackstone.png");
blackStone=ImageIO.read(imageReader);
imageReader.close();

imageReader=this.getClass().getResourceAsStream("/artw
ork/northeastnode.png");
northEastNode=ImageIO.read(imageReader);
imageReader.close();

imageReader=this.getClass().getResourceAsStream("/artw
ork/southeastnode.png");
southEastNode=ImageIO.read(imageReader);
imageReader.close();

imageReader=this.getClass().getResourceAsStream("/artw
ork/southnode.png");
southNode=ImageIO.read(imageReader);
imageReader.close();

imageReader=this.getClass().getResourceAsStream("/artw
ork/southwestnode.png");
southWestNode=ImageIO.read(imageReader);
imageReader.close();

imageReader=this.getClass().getResourceAsStream("/artw
ork/westnode.png");
westNode=ImageIO.read(imageReader);
imageReader.close();

imageReader=this.getClass().getResourceAsStream("/artw
ork/northwestnode.png");
northWestNode=ImageIO.read(imageReader);
imageReader.close();
}
catch(Exception e){
e.printStackTrace();
}
}
/** Creates new form GInterface */
public GraphicalInterface(GraphicalInterfaceController
gic , int maxrow , int maxcolumn) {
this.gic=gic;
MAXROW=maxrow;
MAXCOLUMN=maxcolumn;
imageInitialisation();
initComponents();
newGameButton.setEnabled(false);
saveGameButton.setEnabled(false);
boardCanvas.setSize(450, 450);
boardImage=new BufferedImage(450 , 450 ,
BufferedImage.TYPE_INT_ARGB);
drawAllBackgroundCells();
}

public void drawAllBackgroundCells(){


int row , column;
for(row=1 ; row <= MAXROW ; row++){
for(column=1 ; column <= MAXCOLUMN ;
column++){
drawBackground(row , column);
}
}
flipBackImage();
}
public void drawBackgroundCell(int row , int column){
drawBackground(row+1 , column+1);
flipBackImage();
}
private void drawBackground(int row , int column){
BufferedImage img;
if(row == 1 && column == 1){
img=northWestNode;
}
else if(row == 1 && column==MAXCOLUMN){
img=northEastNode;
}
else if(row==MAXROW && column==1){
img=southWestNode;
}
else if(row==MAXROW && column==MAXCOLUMN){
img=southEastNode;
}
else if(row==1){
img=northNode;
}
else if(row==MAXROW){
img=southNode;
}
else if(column==1){
img=westNode;
}
else if(column==MAXCOLUMN){
img=eastNode;

}
else{
img=middleNode;
}
drawBackImage(img , row , column);
}
public void drawBlackStone(int row , int column){
BufferedImage img=blackStone;
drawBackImage(img , row+1 , column+1);
flipBackImage();
}
public void drawWhiteStone(int row , int column){
BufferedImage img=whiteStone;
drawBackImage(img , row+1 , column+1);
flipBackImage();
}

//System.out.println(cellWidth * (column - 1) + " " +


cellHeight * (row - 1));
}
private void flipBackImage(){
BoardCanvas bc=(BoardCanvas) boardCanvas;
bc.updateBackImage(boardImage);
bc.repaint();
}
private String getFileOnDisk(boolean forLoading){
JFileChooser fc=new JFileChooser();
fc.setFileSelectionMode(JFileChooser.FILES_ONLY);
int result;
File targetFile=null;
if(forLoading){
result=fc.showOpenDialog(this);
}
else{
result=fc.showSaveDialog(this);
}

private void drawBackImage(Image img , int row , int


column){
int width , height;
int cellWidth , cellHeight;
int imageWidth , imageHeight;
int scaledWidth , scaledHeight;
Graphics2D g;
BufferedImage scaledImage;

if(result == JFileChooser.APPROVE_OPTION){
targetFile=fc.getSelectedFile();
}
if(targetFile != null){
return targetFile.getAbsolutePath();
}
return null;

height=boardCanvas.getHeight();
width=boardCanvas.getWidth();
}
cellWidth=width/MAXCOLUMN;
cellHeight=height/MAXROW;
imageWidth=img.getWidth(this);
imageHeight=img.getHeight(this);
g=(Graphics2D) boardImage.getGraphics();
g.setRenderingHint(RenderingHints.KEY_INTERPOLATION ,
RenderingHints.VALUE_INTERPOLATION_BILINEAR);
g.drawImage(img , cellWidth * (column - 1) ,
cellHeight * (row - 1) , cellWidth , cellHeight , this);

/** This method is called from within the constructor to


* initialize the form.
* WARNING: Do NOT modify this code. The content of
this method is
* always regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed"
desc="Generated Code">//GEN-BEGIN:initComponents
private void initComponents() {

boardCanvas = new BoardCanvas();


previousMoveButton = new javax.swing.JButton();
jLabel1 = new javax.swing.JLabel();
nextMoveButton = new javax.swing.JButton();
whitePlayerComboBox = new
javax.swing.JComboBox();
startGameButton = new javax.swing.JButton();
blackPlayerComboBox = new
javax.swing.JComboBox();
newGameButton = new javax.swing.JButton();
saveGameButton = new javax.swing.JButton();
loadGameButton = new javax.swing.JButton();
jLabel2 = new javax.swing.JLabel();
boardCanvas.setBackground(new java.awt.Color(232,
196, 25));
boardCanvas.addMouseListener(new
java.awt.event.MouseAdapter() {
public void
mouseClicked(java.awt.event.MouseEvent evt) {
boardCanvasMouseClicked(evt);
}
public void
mouseExited(java.awt.event.MouseEvent evt) {
boardCanvasMouseExited(evt);
}
});
boardCanvas.addMouseMotionListener(new
java.awt.event.MouseMotionAdapter() {
public void
mouseMoved(java.awt.event.MouseEvent evt) {
boardCanvasMouseMoved(evt);
}
});
previousMoveButton.setText("Previous move");
previousMoveButton.addMouseListener(new
java.awt.event.MouseAdapter() {
public void
mouseClicked(java.awt.event.MouseEvent evt) {
previousMoveButtonMouseClicked(evt);
}
});

jLabel1.setText("black player");
nextMoveButton.setText("Next move");
nextMoveButton.addMouseListener(new
java.awt.event.MouseAdapter() {
public void
mouseClicked(java.awt.event.MouseEvent evt) {
nextMoveButtonMouseClicked(evt);
}
});
whitePlayerComboBox.setModel(new
javax.swing.DefaultComboBoxModel(new String[] {
"Human", "Computer" }));
startGameButton.setText("Start game");
startGameButton.addMouseListener(new
java.awt.event.MouseAdapter() {
public void
mouseClicked(java.awt.event.MouseEvent evt) {
startGameButtonMouseClicked(evt);
}
});
blackPlayerComboBox.setModel(new
javax.swing.DefaultComboBoxModel(new String[] {
"Human", "Computer" }));
newGameButton.setText("New game");
newGameButton.addMouseListener(new
java.awt.event.MouseAdapter() {
public void
mouseClicked(java.awt.event.MouseEvent evt) {
newGameButtonMouseClicked(evt);
}
});
saveGameButton.setText("Save game");
saveGameButton.addMouseListener(new
java.awt.event.MouseAdapter() {
public void
mouseClicked(java.awt.event.MouseEvent evt) {
saveGameButtonMouseClicked(evt);
}

});
loadGameButton.setText("Load game");
loadGameButton.addMouseListener(new
java.awt.event.MouseAdapter() {
public void
mouseClicked(java.awt.event.MouseEvent evt) {
loadGameButtonMouseClicked(evt);
}
});
jLabel2.setText("white player");
javax.swing.GroupLayout layout = new
javax.swing.GroupLayout(this);
this.setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Align
ment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(boardCanvas,
javax.swing.GroupLayout.PREFERRED_SIZE, 450,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(31, 31, 31)
.addGroup(layout.createParallelGroup(javax.swing.GroupL
ayout.Alignment.TRAILING)
.addComponent(previousMoveButton,
javax.swing.GroupLayout.DEFAULT_SIZE, 121,
Short.MAX_VALUE)
.addComponent(jLabel1,
javax.swing.GroupLayout.Alignment.LEADING,
javax.swing.GroupLayout.PREFERRED_SIZE, 103,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(nextMoveButton,
javax.swing.GroupLayout.DEFAULT_SIZE, 121,
Short.MAX_VALUE)
.addComponent(whitePlayerComboBox,
javax.swing.GroupLayout.Alignment.LEADING, 0, 121,
Short.MAX_VALUE)
.addComponent(startGameButton,
javax.swing.GroupLayout.Alignment.LEADING,

javax.swing.GroupLayout.DEFAULT_SIZE, 121,
Short.MAX_VALUE)
.addComponent(blackPlayerComboBox, 0, 121,
Short.MAX_VALUE)
.addComponent(newGameButton,
javax.swing.GroupLayout.DEFAULT_SIZE, 121,
Short.MAX_VALUE)
.addComponent(saveGameButton,
javax.swing.GroupLayout.DEFAULT_SIZE, 121,
Short.MAX_VALUE)
.addComponent(loadGameButton,
javax.swing.GroupLayout.DEFAULT_SIZE, 121,
Short.MAX_VALUE)
.addComponent(jLabel2,
javax.swing.GroupLayout.Alignment.LEADING,
javax.swing.GroupLayout.PREFERRED_SIZE, 95,
javax.swing.GroupLayout.PREFERRED_SIZE))
.addContainerGap())
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Align
ment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(boardCanvas,
javax.swing.GroupLayout.PREFERRED_SIZE, 450,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap())
.addGroup(layout.createSequentialGroup()
.addGap(30, 30, 30)
.addComponent(newGameButton)
.addGap(8, 8, 8)
.addComponent(saveGameButton)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPla
cement.UNRELATED)
.addComponent(loadGameButton)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPla
cement.RELATED, 46, Short.MAX_VALUE)
.addComponent(jLabel1)

.addPreferredGap(javax.swing.LayoutStyle.ComponentPla
cement.RELATED)
.addComponent(blackPlayerComboBox,
javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(8, 8, 8)
.addComponent(jLabel2)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPla
cement.RELATED)
.addComponent(whitePlayerComboBox,
javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(18, 18, 18)
.addComponent(startGameButton)
.addGap(44, 44, 44)
.addComponent(previousMoveButton)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPla
cement.RELATED)
.addComponent(nextMoveButton)
.addGap(40, 40, 40))
);
}// </editor-fold>//GEN-END:initComponents
private void
newGameButtonMouseClicked(java.awt.event.MouseEve
nt evt) {//GENFIRST:event_newGameButtonMouseClicked
// TODO add your handling code here:
if(! newGameButton.isEnabled()){
return;
}
gic.newGameButtonClicked();
}//GEN-LAST:event_newGameButtonMouseClicked
private void
saveGameButtonMouseClicked(java.awt.event.MouseEve
nt evt) {//GENFIRST:event_saveGameButtonMouseClicked
// TODO add your handling code here:

if(! saveGameButton.isEnabled()){
return;
}
String f=getFileOnDisk(false);
if(f != null){
gic.saveFile(f);
}
}//GEN-LAST:event_saveGameButtonMouseClicked
private void
loadGameButtonMouseClicked(java.awt.event.MouseEve
nt evt) {//GENFIRST:event_loadGameButtonMouseClicked
// TODO add your handling code here:
if(! loadGameButton.isEnabled()){
return;
}
String f=getFileOnDisk(true);
if(f != null){
gic.loadFile(f);
}
}//GEN-LAST:event_loadGameButtonMouseClicked
private void
startGameButtonMouseClicked(java.awt.event.MouseEve
nt evt) {//GENFIRST:event_startGameButtonMouseClicked
// TODO add your handling code here:
if(! startGameButton.isEnabled()){
return;
}
boolean blackHuman , whiteHuman;
if(blackPlayerComboBox.getSelectedIndex() == 0){
blackHuman=true;
}
else{
blackHuman=false;
}
if(whitePlayerComboBox.getSelectedIndex() == 0){
whiteHuman=true;
}
else{
whiteHuman=false;

}
gic.startGame(blackHuman , whiteHuman);
}//GEN-LAST:event_startGameButtonMouseClicked
private void
previousMoveButtonMouseClicked(java.awt.event.Mouse
Event evt) {//GENFIRST:event_previousMoveButtonMouseClicked
// TODO add your handling code here:
if(! previousMoveButton.isEnabled()){
return;
}
gic.getPreviousMove();
}//GEN-LAST:event_previousMoveButtonMouseClicked
private void
nextMoveButtonMouseClicked(java.awt.event.MouseEve
nt evt) {//GENFIRST:event_nextMoveButtonMouseClicked
// TODO add your handling code here:
if(! nextMoveButton.isEnabled()){
return;
}
gic.getNextMove();
}//GEN-LAST:event_nextMoveButtonMouseClicked
private void
boardCanvasMouseMoved(java.awt.event.MouseEvent
evt) {//GEN-FIRST:event_boardCanvasMouseMoved
// TODO add your handling code here:
int xPress , yPress;
int canvasWidth , canvasHeight;
int cellWidth , cellHeight;
int row , column;
xPress=evt.getX();
yPress=evt.getY();
canvasWidth=boardCanvas.getWidth();
canvasHeight=boardCanvas.getHeight();
cellWidth=(int)((float)canvasWidth /
(float)MAXROW);

cellHeight=(int)((float)canvasHeight /
(float)MAXCOLUMN);
row=(int) ((float)yPress / (float) cellHeight);
column=(int)((float)xPress / (float)cellWidth);
gic.mouseMoved(row, column);
}//GEN-LAST:event_boardCanvasMouseMoved
private void
boardCanvasMouseClicked(java.awt.event.MouseEvent
evt) {//GEN-FIRST:event_boardCanvasMouseClicked
// TODO add your handling code here:
int xPress , yPress;
int canvasWidth , canvasHeight;
int cellWidth , cellHeight;
int row , column;
xPress=evt.getX();
yPress=evt.getY();
canvasWidth=boardCanvas.getWidth();
canvasHeight=boardCanvas.getHeight();
cellWidth=(int)((float)canvasWidth /
(float)MAXROW);
cellHeight=(int)((float)canvasHeight /
(float)MAXCOLUMN);
row=(int) ((float)yPress / (float) cellHeight);
column=(int)((float)xPress / (float)cellWidth);
gic.mouseClicked(row, column);
}//GEN-LAST:event_boardCanvasMouseClicked
private void
boardCanvasMouseExited(java.awt.event.MouseEvent
evt) {//GEN-FIRST:event_boardCanvasMouseExited
// TODO add your handling code here:
gic.mouseMoved(-1 , -1);
}//GEN-LAST:event_boardCanvasMouseExited

// Variables declaration - do not modify//GENBEGIN:variables


private javax.swing.JComboBox blackPlayerComboBox;
private java.awt.Canvas boardCanvas;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
public javax.swing.JButton loadGameButton;
public javax.swing.JButton newGameButton;
public javax.swing.JButton nextMoveButton;
public javax.swing.JButton previousMoveButton;
public javax.swing.JButton saveGameButton;
public javax.swing.JButton startGameButton;
private javax.swing.JComboBox whitePlayerComboBox;
// End of variables declaration//GEN-END:variables
}

You might also like