Sei sulla pagina 1di 4

import java.awt.*; import java.awt.event.*; import javax.swing.

*; class Game extends Frame implements MouseListener{ static String player; String player1,player2; String s1,s2; static boolean b=true; Panel p=new Panel(); Panel c=new Panel(); int a[]=new int[9]; //for back hand comparison Panel t[]=new Panel[9]; Label l=new Label(); Label l1=new Label(); Label l2[]=new Label[9]; /******************constructor for Frame of Game class*********************/ Game(){ player1=JOptionPane.showInputDialog("Player 1 Enter Your Name"); player2=JOptionPane.showInputDialog("Player 2 Enter Your Name"); s1=JOptionPane.showInputDialog(player1+" Choose any single Character as Your Sign"); s2=JOptionPane.showInputDialog(player2+" Choose any single Character as Your Sign"); if(player1.equals(player2)) player2=player2+" "; player=player1; p.setForeground(Color.white); p.setFont(new Font("comic sans ms",Font.BOLD,20)); add(p,BorderLayout.NORTH); add(c,BorderLayout.CENTER); setBackground(Color.red); p.setBackground(Color.red); c.setBackground(Color.red); c.setLayout(new GridLayout(3,3,3,3)); l.setText(player+"'s Turn"); l1.setText(s1); p.add(l); p.add(l1); for(int i=0;i<9;i++){ t[i]=new Panel(); t[i].setLayout(new BorderLayout()); t[i].setBackground(Color.blue); t[i].setForeground(Color.cyan); t[i].setFont(new Font("comic sans ms",Font.BOLD,80)); l2[i]=new Label(); t[i].add(l2[i],BorderLayout.CENTER); l2[i].addMouseListener(this);

c.add(t[i]); } for(int i=0;i<9;i++) a[i]=i+3; // array is given dummy values other than 1 and 2 } /*******sign of the current player is drawn on canvas when clicked*********/ public void mouseClicked(MouseEvent e){ for(int i=0;i<9;i++){ if(e.getSource()==l2[i]){ if(a[i]>=3){ /*checked if that index already is filled with either 1 or 2 means used before than to not allow Player to give new value*/ if(b){ a[i]=1;// assigned 1 for Player1 b=false; l2[i].setText(s1); } else{ a[i]=2;//assigned 2 for Player1 b=true; l2[i].setText(s2); } } } } /*In case any player has drawn consecutive signs then the result is Shown by Dialog box*/ Dialog d=new Dialog(this,"Result"); Button bb=new Button("Quit"); d.setBounds(300,80,250,100); // set size and location of Dialog Box on screen d.setModal(true); d.setResizable(false); bb.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e){ System.exit(0); } }); d.setFont(new Font("comic sans ms",Font.BOLD,18)); d.setBackground(Color.magenta); d.setForeground(Color.white); d.setLayout(new FlowLayout()); Label ll=new Label(); d.add(ll,BorderLayout.CENTER);

bb.setBackground(Color.black); d.add(bb); /*******Condition to check if any player has drawn consecutive Signs*******/ /** Any Consecutive index according to postion of blocks having same value*/ if((a[0]==a[1] && a[0]==a[2])||(a[3]==a[4] && a[3]==a[5]) ||(a[6]==a[7] && a[6]==a[8]) ||(a[0]==a[3] && a[0]==a[6]) ||(a[1]==a[4] && a[1]==a[7]) ||(a[2]==a[5] && a[2]==a[8]) ||(a[0]==a[4] && a[0]==a[8]) ||(a[2]==a[4] && a[2]==a[6])){ ll.setText(player+" is the winner "); d.setVisible(true); } /*************to check if all blocks are filled and both player fail to draw consecutive signs then game is over****************************************/ else{ int count=0; for(int i=0;i<9;i++) if(a[i]<3) count++; if(count==9){ ll.setText("Game is Over"); d.setVisible(true); } } /******Sign of current Player is shown and charge of game is exchanged*****/ if(b){ player=player1; l1.setText(s1); } else{ player=player2; l1.setText(s2); } l.setText(player+"'s Turn"); } /* Incomplete Methods of MouseListener are Completed here but are not used*/ public void mousePressed(MouseEvent e){} public void mouseReleased(MouseEvent e){} public void mouseEntered(MouseEvent e){} public void mouseExited(MouseEvent e){} /*******************************Main is here*******************************/ public static void main(String arg[]){ Game g=new Game(); g.setTitle("Game of Tick and Cross by Abaid");

g.setResizable(false); g.setBounds(200,150,350,350); g.setVisible(true); } }

// set size and location of frame on screen

Potrebbero piacerti anche