The Artima Developer Community
Sponsored Link

Java Answers Forum
draw situation not working in game of tictac toe in java

1 reply on 1 page. Most recent reply: Dec 3, 2015 4:06 AM by Sahaj Jain

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 1 reply on 1 page
h p

Posts: 1
Nickname: hp27
Registered: Jul, 2015

draw situation not working in game of tictac toe in java Posted: Aug 2, 2015 2:38 AM
Reply to this message Reply
Advertisement
Friends, I am just a beginner in java. I was making a game of tic tac toe.This is what i made. The draw situation is not working. Whenever a draw situation comes, the game simply hangs.Please tell me is there any mistake in my code?

import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;
 
class tictactoe
{
	public static void main(String abc[])
	{
		tframe1 tf1=new tframe1();
		tf1.setVisible(true);
	}
}
 
class tframe1 extends Frame
{
	boolean w=false;
	boolean aa,bb,cc,dd,ee,ff,gg,hh,ii;
	//these will be true for panel containing X and false otherwise
 
	int replay;
 
	boolean a,b,c,d,e,f,g,h,i;
	//these will be true for the panel containing O and false otherwise
 
	//these will be true when the respective panel has already been used
	boolean f_1=false;
	boolean f_2=false;
	boolean f_3=false;
	boolean f_4=false;
	boolean f_5=false;
	boolean f_6=false;
	boolean f_7=false;
	boolean f_8=false;
	boolean f_9=false;
 
	Panel p1=new Panel();
	Panel p2=new Panel();
	Panel p3=new Panel();
	Panel p4=new Panel();
	Panel p5=new Panel();
	Panel p6=new Panel();
	Panel p7=new Panel();
	Panel p8=new Panel();
	Panel p9=new Panel();
 
	Label l1=new Label("",1);
	Label l2=new Label("",1);
	Label l3=new Label("",1);
	Label l4=new Label("",1);
	Label l5=new Label("",1);
	Label l6=new Label("",1);
	Label l7=new Label("",1);
	Label l8=new Label("",1);
	Label l9=new Label("",1);
 
	Font f1=new Font("Arial",1,20);
 
	tframe1()
	{
		setLayout(new GridLayout(3,3,10,10));
		setTitle("Tic Tac Toe");
		setSize(300,300);
		setBackground(Color.black);
 
		addWindowListener(
			new WindowAdapter()
			{
				public void windowClosing(WindowEvent e)
				{
					System.exit(0);
				}
			});
 
 
		add(p1);
		Color c=new Color(255,255,255);
		p1.setBackground(c);
		add(p2);
		p2.setBackground(c);
		add(p3);
		p3.setBackground(c);
		add(p4);
		p4.setBackground(c);
		add(p5);
		p5.setBackground(c);
		add(p6);
		p6.setBackground(c);
		add(p7);
		p7.setBackground(c);
		add(p8);
		p8.setBackground(c);
		add(p9);
		p9.setBackground(c);
 
		l1.setFont(f1);
		l2.setFont(f1);
		l3.setFont(f1);
		l4.setFont(f1);
		l5.setFont(f1);
		l6.setFont(f1);
		l7.setFont(f1);
		l8.setFont(f1);
		l9.setFont(f1);
 
		p1.addMouseListener(
			new MouseAdapter()
			{
				public void mouseClicked(MouseEvent e)
				{
					f1();
				}
			});
 
		p2.addMouseListener(
			new MouseAdapter()
			{
				public void mouseClicked(MouseEvent e)
				{
					f2();
				}
			});
 
		p3.addMouseListener(
			new MouseAdapter()
			{
				public void mouseClicked(MouseEvent e)
				{
					f3();
				}
			});
 
		p4.addMouseListener(
			new MouseAdapter()
			{
				public void mouseClicked(MouseEvent e)
				{
					f4();
				}
			});
 
		p5.addMouseListener(
			new MouseAdapter()
			{
				public void mouseClicked(MouseEvent e)
				{
					f5();
				}
			});
 
		p6.addMouseListener(
			new MouseAdapter()
			{
				public void mouseClicked(MouseEvent e)
				{
					f6();
				}
			});
 
		p7.addMouseListener(
			new MouseAdapter()
			{
				public void mouseClicked(MouseEvent e)
				{
					f7();
				}
			});
 
		p8.addMouseListener(new MouseAdapter()
		{
			public void mouseClicked(MouseEvent e)
			{
				f8();
			}
		});
 
		p9.addMouseListener(new MouseAdapter()
		{
			public void mouseClicked(MouseEvent e)
			{
				f9();
			}
		});
 
		l1.setBounds(5,5,95,95);
		l2.setBounds(5,5,95,95);
		l3.setBounds(5,5,95,95);
		l4.setBounds(5,5,95,95);
		l5.setBounds(5,5,95,95);
		l6.setBounds(5,5,95,95);
		l7.setBounds(5,5,95,95);
		l8.setBounds(5,5,95,95);
		l9.setBounds(5,5,95,95);
 
		p1.add(l1);
		p2.add(l2);
		p3.add(l3);
		p4.add(l4);
		p5.add(l5);
		p6.add(l6);
		p7.add(l7);
		p8.add(l8);
		p9.add(l9);
	}
	void f1()
	{
		if(!(f_1||w))
		{
			a=true;
			l1.setText("O");
			win();
 
			f_1=true;
			comp();
		}
	}
	void f2()
	{
		if(!(f_2||w))
		{
			b=true;
			l2.setText("O");
			win();
			f_2=true;
 
			comp();
		}
	}
	void f3()
	{
		if(!(f_3||w))
		{
			c=true;
			l3.setText("O");
			win();
 
			f_3=true;
			comp();
		}
	}
	void f4()
	{
		if(!(f_4||w))
		{
			d=true;
			l4.setText("O");
			win();
 
			f_4=true;
			comp();
		}
	}
	void f5()
	{
		if(!(f_5||w))
		{
			e=true;
			l5.setText("O");
			win();
 
			f_5=true;
			comp();
		}
	}
	void f6()
	{
		if(!(f_6||w))
		{
			f=true;
			l6.setText("O");
			win();
 
			f_6=true;
			comp();
		}
	}
	void f7()
	{
		if(!(f_7||w))
		{
			g=true;
			l7.setText("O");
			win();
 
			f_7=true;
			comp();
		}
	}
	void f8()
	{
		if(!(f_8||w))
		{
			h=true;
			l8.setText("O");
			win();
 
			f_8=true;
			comp();
		}
	}
 
	void f9()
	{
		if(!(f_9||w))
		{
			i=true;
			l9.setText("O");
			win();
		
			f_9=true;
			comp();
		}
	}
		void win()
		{
			if((a&&b&&c)||(d&&e&&f)||(g&&h&&i)||(a&&d&&g)||(b&&e&&h)||(c&&f&&i)||(a&&e&&i)||(c&&e&&g))
			{
				w=true;
				JOptionPane.showMessageDialog(this,"You win!!");
 
				end();
			}
			draw();
 
		}
		void lose()
		{
			if((aa&&bb&&cc)||(dd&&ee&&ff)||(gg&&hh&&ii)||(aa&&dd&&gg)||(bb&&ee&&hh)||(cc&&ff&&ii)||(aa&&ee&&ii)||(cc&&ee&&gg))
			{
				w=true;
				JOptionPane.showMessageDialog(this,"Computer wins!!");
 
				end();
			}
			draw();
		}
		void end()
		{
			replay=JOptionPane.showConfirmDialog(this,"Do you want to replay the game?");
			if(replay==1)System.exit(0);
			if(replay==0)
			{
				dispose();
				new tframe1().setVisible(true);
			}
		}
		void draw()
		{
			
			if((f_1)&&(f_2)&&(f_3)&&(f_4)&&(f_5)&&(f_6)&&(f_7)&&(f_8)&&(f_9)&&(!w))
			{
				w=true;
				JOptionPane.showMessageDialog(this,"Game Tie");
				end();
			}
		}
		void comp()
		{
			int k=0;
			if(!w)
			do{
				Random r=new Random();
				double oneToNine=r.nextInt(9)+1;
 
				if((oneToNine==1)&&(!f_1))
				{
					f_1=true;
					l1.setText("X");
					aa=true;
					k=1;
					lose();
				
				}
				if((oneToNine==2)&&(!f_2))
				{
					f_2=true;
					l2.setText("X");
					bb=true;
					k=1;
					lose();
				
				}
				if((oneToNine==3)&&(!f_3))
				{
					f_3=true;
					l3.setText("X");
					k=1;
					cc=true;
					lose();
				
				}
				if((oneToNine==4)&&(!f_4))
				{
					f_4=true;
					l4.setText("X");
					dd=true;
					k=1;
					lose();
				
				}
				if((oneToNine==5)&&(!f_5))
				{
					f_5=true;
					l5.setText("X");
					k=1;
					ee=true;
					lose();
				
				}
				if((oneToNine==6)&&(!f_6))
				{
					f_6=true;
					l6.setText("X");
					ff=true;
					k=1;
					lose();
				
				}
				if((oneToNine==7)&&(!f_7))
				{
					f_7=true;
					l7.setText("X");
					gg=true;
					k=1;
					lose();
				
				}
				if((oneToNine==8)&&(!f_8))
				{
					f_8=true;
					l8.setText("X");
					hh=true;
					lose();
					k=1;
				
				}
				if((oneToNine==9)&&(!f_9))
				{
					f_9=true;
					l9.setText("X");
					ii=true;
					lose();
					k=1;
			
				}
			}while(k==0);
		}
	}
 


Sahaj Jain

Posts: 1
Nickname: sahajjain
Registered: Dec, 2015

Re: draw situation not working in game of tictac toe in java Posted: Dec 3, 2015 4:06 AM
Reply to this message Reply
You need to move
f_*=true expression before win() method call

void f1()
{
if(!(f_1||w))
{
a=true;
l1.setText("O");
f_1=true;
win();

// f_1=true;
comp();
}
}

Flat View: This topic has 1 reply on 1 page
Topic: Java Previous Topic   Next Topic Topic: Parameters in class are objects from another class

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use