Results 1 to 2 of 2
  1. #1
    vito300 is offline Member
    Join Date
    Nov 2008
    Posts
    1
    Rep Power
    0

    Default Help Tic Tac Toe!!

    I need a help...
    I do not succeed to plan the rules of the game...
    please help!!

    This is the View...
    Java Code:
    import java.awt.Color;
    import java.awt.Dimension;
    import java.awt.GridLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.util.Observable;
    import java.util.Observer;
    
    import javax.swing.JButton;
    import javax.swing.JOptionPane;
    import javax.swing.JPanel;
    
    
    public class TrisView extends JPanel implements Observer{
    	TrisModel m;
    	TrisControl c;
    	private JButton []pulsanti;
    	
    	
    	
    	public TrisView(TrisModel m,final TrisControl c){
    		this.m=m;
    		this.c=c;
    		m.addObserver(this);
    		this.setPreferredSize(new Dimension(300,300));
    		this.setLayout(new GridLayout(3,3));
    		
    		pulsanti=new JButton[10];
    		for(int i=0;i<9;i++){
    			pulsanti[i]=new JButton();
    			final int i1=i;
    			pulsanti[i].addActionListener(
    					new ActionListener(){
    						public void actionPerformed(ActionEvent arg0) {
    							// TODO Auto-generated method stub
    							c.CambiaStato (i1);
    						}
    					});
    				this.add(pulsanti[i]);
    			}
    		}
    	
    
    	public void update(Observable arg0, Object arg1) {
    		// TODO Auto-generated method stub
    		String azione=(String)arg1;
    		if(azione=="Aggiorna");
    		for(int i=0;i<9;i++){
    			int valore=m.getStato(i);
    			
    			if(valore==0){
    				pulsanti[i].setText(m.getX());
    				pulsanti[i].setEnabled(false);
    				//m.Combinazioni(i);
    			}
    			if(valore==2){
    				pulsanti[i].setText(m.getO());
    				pulsanti[i].setEnabled(false);
    				//m.Combinazioni(i);
    			}
    		}
    		//for(int i=0;i<9;i++){
    		 //if(m.isWin()==true){
    				//m.Combinazioni(i);
    			//	JOptionPane.showMessageDialog(this,"Hai Vinto","Complimentoni",JOptionPane.INFORMATION_MESSAGE);
    			//}
    		 //else if(m.getCount()==9 && m.isWin()==false){
    			// JOptionPane.showMessageDialog(this,"Hai Perso","Sfigato!",JOptionPane.INFORMATION_MESSAGE );
    		 //}
    			 
    			
    			
    		}
    	}
    this is the Model...
    Java Code:
    port java.util.Observable;
    
    import javax.swing.JButton;
    
    
    public class TrisModel extends Observable{
    	private int count=0;
    	private String lettera="";
    	private int []pulsanti;
    	private int [][] combinazioni={
    			{0,1,2},{3,4,5},{6,7,8}, //orizzontali
    		{0,3,6},{1,4,7},{2,5,8},//verticali
    			{0,4,8},{2,4,6}//diagonali
    			
    	};
    private String x,o;
    private boolean win=true;
    	
    	public TrisModel(){
    		pulsanti=new int[9];
    		x=new String("X");
    		o=new String("O");
    	
    		for(int i=0;i<9;i++){
    			pulsanti[i]=1;
    		}
    	}
    
    		
    		public void Seleziona(int i){
    			count++;
    			if(count==1 ||count==3||count==5||count==7||count==9){
    				pulsanti[i]=0;
    			}
    			else if(count==2||count==4||count==6||count==8){
    				pulsanti[i]=2;
    	}
    			setChanged();
    			notifyObservers("Aggiorna");
    		}	
    			
    //public boolean Combinazioni(int i){
    	//count++;
    	
    //	if(pulsanti[9]==combinazioni[i][0] &&
    	//		pulsanti[9]==combinazioni[i][1] &&
    		//	pulsanti[9]==combinazioni[i][2])
    			//                             {
    		//return win= true;
    		//	                             }
    //	else return false;
    	//		                             }
    
    
    	
    		public int getStato(int i){
    			return pulsanti[i];
    		}
    
    
    		public String getX() {
    			return x;
    		}
    
    
    	
    
    		public boolean isWin() {
    			return win;
    		}
    
    
    	
    
    
    		public String getO() {
    			return o;
    		}
    
    
    		public int getCount() {
    			// TODO Auto-generated method stub
    			return count;
    		}
    
    
    }
    this the Control...
    Java Code:
    port java.awt.EventQueue;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    
    import javax.swing.JFrame;
    
    
    public class TrisControl implements ActionListener{
    	private TrisModel m;
    	private TrisView v;
    	private JFrame f;
    	
    	
    	public TrisControl(){
    		m=new TrisModel();
    		v=new TrisView(m,this);
    		f=new JFrame("Gioca al Tris");
    		f.add(v);
    		f.setVisible(true);
    		f.pack();
    		f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    		
    	}
    	public static void main(String args[]){
    		Runnable r=new Runnable(){
    			public void run(){
    				new TrisControl();
    			}
    		};
    		EventQueue.invokeLater(r);
    	}
    	public void actionPerformed(ActionEvent arg0) {
    		// TODO Auto-generated method stub
    		
    	}
    	public void CambiaStato(int i) {
    		// TODO Auto-generated method stub
    		m.Seleziona(i);
    		//m.Combinazioni(i);
    	}
    
    }

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Consider giving your model a boolean variable say called xTurn to see if it is X's turn or not, a boolean called win to see if the game is over yet, both with suitable getter methods. Consider adding a checkWin method that iterates through the arrays to see if the cells all contain the same thing, X or O. Finally consider giving your model a toString method and a main method so that you can test and debug it separate from the view or the controller.

    One thing I did when doing this before was to create a Cell object that encapsulates the states of a tic tac toe cell: occupied or not, holds X or not, able to reset, has a toString method. Then I had a model hold a 2-dimensional grid of these creatures. Here's what my Model's interface looked like:
    Java Code:
    import javax.swing.event.ChangeListener;
    
    public interface TttModelable
    {
    
      public abstract void reset();
    
      public abstract boolean isXTurn();
      
      public abstract int getLastRow();
      
      public abstract int getLastCol();
    
      public abstract boolean isWin();
      
      public abstract void addChangeListener(ChangeListener cl);
    
      public abstract void takeTurn(int col, int row);
    
      public abstract boolean checkWin(int col, int row, TttXO xo);
    
    }
    The heart of my model was the checkWin method (note that the TttXo variable was an enum to encapsulate X and O',s though a boolean isX would work as well since it only holds two states). The win simply iterates through the rows, columns and diaganols associated with the last cell entered and checked to see if filled with all the same TttXO objects.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •