Java Tic Tac Toe Using MVC and Singleton

Java Tic Tac Toe Using MVC and Singleton



-----------------------------------------CODE--------------------------------------------------


//*********************************************
//AUTHOR - THE JAVA GEORDIE
//CREATES INSTANCE OF CONTROLLER
//*********************************************

class start
{
public static void main(String args[])
{
controller cont = new controller();
}
}

--------------------------------------------------------------------------------


//*********************************************
// AUTHOR - THE JAVA GEORDIE
// CLASS TO WORK AS THE VIEW
// USES SINGLETON PATTERN FOR GUI
// 
// CHECKS TO SEE WHOS GO IS NEXT
//*********************************************

import javax.swing.JFrame;
import javax.swing.JButton;
import java.awt.GridLayout;


class view extends JFrame
{
// DEFINE BUTTONS AND GRID
private static view firstInstance =null;
protected JButton but[];
protected GridLayout grid;



protected view()
{
super("Tic Tac Toe Game");
but = new JButton[9];
grid= new GridLayout(3,3,5,5);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(250,250);
setLayout(grid);

//BUILD BUTTONS
for(int loop=0; loop<9; loop++)
{
but[loop] = new JButton();
add(but[loop]);
}
setVisible(true);
}


// RESET TO GUI
public void resetGame()
{
for(int z=0; z<9; z++)
{
but[z].setText("");
}
}

//USE THE SINGLETON DESIGN PATTERN
public static view getInstace()
{
if (firstInstance==null)
firstInstance = new view();

return firstInstance;
}
}
-------------------------------------------------------------------------------


//*********************************************
// AUTHOR - THE JAVA GEORDIE
// CLASS TO WORK AS THE GLUE BETWEEN VIEW AND MODEL
// PASSES VALUES FROM VIEW TO MODEL
// ADDS LISTENERS TO THE VIEW
// CHECKS TO SEE WHOS GO IS NEXT
//*********************************************

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JOptionPane;

class controller extends view implements ActionListener
{
private model newModel = new model();
private int result=0;
private boolean whosGo=false; // SET PLAYER O TO GO FIRST

public controller()
{

//ADD ACTIONLISTER TO BUTTONS FROM VIEW CLASS
for(int x=0; x<9; x++)
super.but[x].addActionListener(this);
}


public void goAgain()
{
int reply = JOptionPane.showConfirmDialog
(null, "PLAY AGAIN ?", "TIC TAC TOE" , JOptionPane.YES_NO_OPTION);

if (reply == JOptionPane.YES_OPTION)
{
newModel.resetGame();
super.resetGame();
}
else
{
dispose();
            System.exit(0);
}
}


public void actionPerformed (ActionEvent event)
{
Object objClicked=event.getSource();

for(int x=0; x<9; x++)
{
if(objClicked==super.but[x])
{
if (super.but[x].getText()=="")
{
if (whosGo==false)
{

super.but[x].setText("X");
newModel.setClick(x,1);
whosGo=true;  // CHANGE TO NEXT PLAYER

result=newModel.getResult();

}
else
{
super.but[x].setText("O");
newModel.setClick(x,2);
whosGo=false; // CHANGE TO NEXT PLAYER

result=newModel.getResult();
}
}
}
}
if(result!=0)
 goAgain();

}
--------------------------------------------------------------------


//*********************************************
//AUTHOR - THE JAVA GEORDIE
//CLASS TO WORK ON THE BUSINESS END OF THE GAME
//SETS UP ARRAY AND DEFINES SOME METHODS TO
//RESET ARRAY - MODIFY THE ARRAY - CHECK RESULTS
//*********************************************

class model
{
int array[] = new int[9]; // O = 1 - X = 2
int numberOfClicks=1;

public void setClick(int position, int XorO)
{
array[position]=XorO; // ADD 1 OR 2 TO ARRAY POSITION
numberOfClicks++; // INCREMENT GAME TURNS

}


//RESET ARRAY AND THE NUMBER OF TURNS COUNT
public void resetGame()
{
numberOfClicks=0;
for(int z=0; z<9; z++)
{
array[z]=0;
}
}

// THIS METHOD CHECKS TO SEE WHICH PLAYER HAS WON IF ANY AND
// RETURN A VAULE 
// 1(IF O WINS) - 2(IF X WINS) - 3(IF NO ONE WINS) - 0 BY DEFAULT
public int getResult()
{
//HORI TEST

if (array[0]==1 && array[1]==1 && array[2]==1)
return 1; // O WINS
if (array[0]==2 && array[1]==2 && array[2]==2)
return 2; // X WINS
if (array[3]==1 && array[4]==1 && array[5]==1)
return 1; // O WINS
if (array[3]==2 && array[4]==2 && array[5]==2)
return 2; // X WINS
if (array[6]==1 && array[7]==1 && array[8]==1)
return 1; // O WINS
if (array[6]==2 && array[7]==2 && array[8]==2)
return 2; // X WINS


// VERT TEST

if (array[0]==1 && array[3]==1 && array[6]==1)
return 1; // O WINS
if (array[0]==2 && array[3]==2 && array[6]==2)
return 2; // X WINS
if (array[1]==1 && array[4]==1 && array[7]==1)
return 1; // O WINS
if (array[1]==2 && array[4]==2 && array[7]==2)
return 2; // X WINS
if (array[2]==1 && array[5]==1 && array[8]==1)
return 1; // O WINS
if (array[2]==2 && array[5]==2 && array[8]==2)
return 2; // X WINS

//DIAGONAL TEST

if (array[0]==1 && array[4]==1 && array[8]==1)
return 1; // O WINS
if (array[0]==2 && array[4]==2 && array[8]==2)
return 2; // X WINS
if (array[2]==1 && array[4]==1 && array[6]==1)
return 1; // O WINS
if (array[2]==2 && array[4]==2 && array[6]==2)
return 2; // X WINS

if (numberOfClicks==9)
return 3; //NO ONE WINS THIS GAME

return 0; // NOBODY HAS WON YET
}
}

---------------------------OUTPUT ------------------------------

JAVA EXAMPLE TIC TAC TOE





No comments:

Post a Comment