Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.awt.*;
- import java.awt.event.*;
- import java.applet.Applet;
- public class TicTacToe extends Applet implements ActionListener {
- /**
- *
- */
- private static final long serialVersionUID = 1831218949168974884L;
- /**
- *
- */
- Button squares[];
- Button newGameButton;
- Label score;
- int emptySquaresLeft=9;
- public void init(){
- //add manager location applet, font and color
- this.setLayout(new BorderLayout());
- this.setBackground(Color.CYAN);
- //change font applet on bold and had size 20
- Font appletFont = new Font("Monospased", Font.BOLD, 20);
- this.setFont(appletFont);
- //create button "NewGame" and register it action listener
- newGameButton = new Button("new game");
- newGameButton.addActionListener(this);
- Panel topPanel = new Panel();
- topPanel.add(newGameButton);
- this.add(topPanel, "North");
- Panel centerPanel = new Panel();
- centerPanel.setLayout(new GridLayout(3,3));
- this.add(centerPanel, "Center");
- score = new Label("You turn");
- this.add(score, "South");
- //create massive for store links on 9 buttons
- squares = new Button[9];
- //create buttons, save links on massive, register them action listner,
- //color it in orange and add on panel
- for(int i=0;i<9;i++){
- squares[i] = new Button();
- squares[i].addActionListener(this);
- squares[i].setBackground(Color.ORANGE);
- centerPanel.add(squares[i]);
- }
- }
- public void actionPerfomed(ActionEvent e){
- Button theButton = (Button) e.getSource();
- //it button NewGame?
- if(theButton == newGameButton){
- for(int i=0;i<9;i++){
- squares[i].setEnabled(true);
- squares[i].setLabel("");
- squares[i].setBackground(Color.ORANGE);
- }
- emptySquaresLeft=9;
- score.setText("You turn!");
- newGameButton.setEnabled(false);
- return; //exit method
- }
- String winner = "";
- //it one of cells?
- for(int i=0;i<9;i++){
- if (theButton == squares[i]){
- squares[i].setLabel("X");
- winner = lookForWinner();
- if(!"".equals(winner)){
- endTheGame();
- }else{
- computerMove();
- winner = lookForWinner();
- if(!"".equals(winner)){
- endTheGame();
- }
- }
- break;
- }
- } //end of for
- if(winner.equals("X")){
- score.setText("You won");
- }else if(winner.equals("O")){
- score.setText("lost");
- }else if (winner.equals("T")){
- score.setText("its a tie");
- }
- }//end action perfomed method
- /*this method call after move, for know who is winner.
- *he checks each rows, for find same cell.
- * @retErn "X", "O", "T" - tie. "" - no winner
- */
- String lookForWinner(){
- String theWinner = "";
- emptySquaresLeft--;
- if(emptySquaresLeft==0){
- return "T"; //tie
- }
- //cheek row 1 - massive elements 0,1,2
- if(!squares[0].getLabel().equals("") &&
- squares[0].getLabel().equals(squares[1].getLabel()) &&
- squares[0].getLabel().equals(squares[2].getLabel())){
- theWinner = squares[0].getLabel();
- highlightWinnrer(0,1,2);
- //chek row 2 - massive elements 3,4,5
- }else if(!squares[3].getLabel().equals("") &&
- squares[3].getLabel().equals(squares[4].getLabel()) &&
- squares[3].getLabel().equals(squares[5].getLabel())){
- theWinner = squares[3].getLabel();
- highlightWinnrer(3,4,5);
- }else if(!squares[6].getLabel().equals("") &&
- squares[6].getLabel().equals(squares[7].getLabel()) &&
- squares[6].getLabel().equals(squares[8].getLabel() )) {
- theWinner = squares[6].getLabel();
- highlightWinnrer(6,7,8);
- }else if(!squares[0].getLabel().equals("") &&
- squares[0].getLabel().equals(squares[3].getLabel()) &&
- squares[0].getLabel().equals(squares[6].getLabel())){
- theWinner = squares[0].getLabel();
- highlightWinnrer(0,3,6);
- }else if(!squares[1].getLabel().equals("") &&
- squares[1].getLabel().equals(squares[4].getLabel()) &&
- squares[1].getLabel().equals(squares[7].getLabel())){
- theWinner = squares[1].getLabel();
- highlightWinnrer(1,4,7);
- }else if(!squares[2].getLabel().equals("") &&
- squares[2].getLabel().equals(squares[5].getLabel()) &&
- squares[2].getLabel().equals(squares[8].getLabel())){
- theWinner = squares[2].getLabel();
- highlightWinnrer(2,5,8);
- }else if(!squares[0].getLabel().equals("") &&
- squares[0].getLabel().equals(squares[4].getLabel()) &&
- squares[0].getLabel().equals(squares[8].getLabel())){
- theWinner = squares[0].getLabel();
- highlightWinnrer(0,4,8);
- }else if(!squares[2].getLabel().equals("") &&
- squares[2].getLabel().equals(squares[4].getLabel()) &&
- squares[2].getLabel().equals(squares[6].getLabel())){
- theWinner = squares[2].getLabel();
- highlightWinnrer(2,4,6);
- }
- return theWinner;
- }
- /**
- * this method applies set of rules, for look best computer move
- * if best move didn't find, selected a random cell.
- */
- void computerMove(){
- int selectedSquare;
- //at first computer try find empty cell beside
- //2 zero's, for victory.
- selectedSquare = findEmptySquare("O");
- //if he can't find two zero, then at last try not to give opponent
- //make row of 3 crosses, placing zero beside crosses
- if(selectedSquare == -1){
- selectedSquare = findEmptySquare("X");
- }
- //if selectedSquare == -1, then try take center cell
- if((selectedSquare == -1) && (squares[4].getLabel().equals("")) ){
- selectedSquare = 4;
- }
- //didn't luck with central cell, take a random
- if(selectedSquare == -1){
- selectedSquare = getRandomSquare();
- }
- squares[selectedSquare].setLabel("O");
- }
- /**
- * this method cheek all row, column and diagonal for know, whether it two cells
- * with a both inscriptions and empty cell.
- * @param give X - for user and O - for computer
- * @return the number of free cells, or -1 if didn't find two cell with same inscription
- */
- int findEmptySquare(String player){
- int weight[] = new int[9];
- for( int i=0; i<9; i++){
- if(squares[i].getLabel().equals("O") )
- weight[i] = -1;
- else if (squares[i].getLabel().equals("X") )
- weight[i] = 1;
- else
- weight[i] = 0;
- }
- int twoWeights = player.equals("O") ? -2 : 2;
- //cheek if in first row two identical and one empty cell
- if(weight[0] + weight[1] + weight[2] == twoWeights){
- if(weight[0] == 0)
- return 0;
- else if (weight[1] == 0)
- return 1;
- else
- return 2;
- }
- //cheek if in two row two identical and one empty cell
- if(weight[3] + weight[4] + weight[5] == twoWeights){
- if(weight[3] == 0)
- return 3;
- else if (weight[4] == 0)
- return 4;
- else
- return 5;
- }
- //and row three
- if(weight[6] + weight[7] + weight[8] == twoWeights){
- if(weight[6] == 0)
- return 6;
- else if (weight[7] == 0)
- return 7;
- else
- return 8;
- }
- //now column 1
- if(weight[0] + weight[3] + weight[6] == twoWeights){
- if(weight[0] == 0)
- return 0;
- else if (weight[3] == 0)
- return 3;
- else
- return 6;
- }
- //column 2
- if(weight[1] + weight[4] + weight[7] == twoWeights){
- if(weight[1] == 0)
- return 1;
- else if (weight[4] == 0)
- return 4;
- else
- return 7;
- }
- //and 3
- if(weight[2] + weight[5] + weight[8] == twoWeights){
- if(weight[2] == 0)
- return 2;
- else if (weight[5] == 0)
- return 5;
- else
- return 8;
- }
- //now diagonal
- if(weight[0] + weight[4] + weight[8] == twoWeights){
- if(weight[0] == 0)
- return 0;
- else if (weight[4] == 0)
- return 4;
- else
- return 8;
- }
- //and two diagonal
- if(weight[2] + weight[4] + weight[6] == twoWeights){
- if(weight[2] == 0)
- return 2;
- else if (weight[4] == 0)
- return 4;
- else
- return 6;
- }
- //didn't find two identical cells
- return -1;
- }//end findEmptySquare();
- int getRandomSquare(){
- boolean gotEmptySquare = false;
- int selectedSquare = -1;
- do{
- selectedSquare = (int) (Math.random()*9);
- if(squares[selectedSquare].getLabel().equals("")){
- gotEmptySquare = true; //for finish cycle
- }
- } while (!gotEmptySquare);
- return selectedSquare;
- }//end getRandom method
- /**
- * method selected winning line.
- * @param 1,2,3 cells for release
- */
- void highlightWinnrer(int win1, int win2, int win3){
- squares[win1].setBackground(Color.CYAN);
- squares[win2].setBackground(Color.CYAN);
- squares[win3].setBackground(Color.CYAN);
- }
- //after game all cells freeze. button "new game" - enabled;
- void endTheGame(){
- newGameButton.setEnabled(true);
- for(int i=0;i<9;i++){
- squares[i].setEnabled(false);
- }
- }
- @Override
- public void actionPerformed(ActionEvent e) {
- // TODO Auto-generated method stub
- }
- }//end
Advertisement
Add Comment
Please, Sign In to add comment