Advertisement

Tic Tac Toe Help!

Started by July 27, 2006 11:23 AM
9 comments, last by eat2thepieseye 18 years, 3 months ago
About a year ago I made a tic-tac-toe game that learned as you played it, so that it started out dumb and ended up becomeing unbeatable if you played it for hours. You can find it at http://www.nisk.k12.ny.us/faculty/baciewicz/GridGames/TTTAI. The source (in java) is
import java.awt.*;import java.awt.event.*;import java.util.*;import java.applet.*;import javax.swing.*;public class TTTAI extends Applet implements MouseListener,ActionListener{	String[][] board,test;	HashMap badMoves=new HashMap();	HashMap goodMoves=new HashMap();	HashMap tieMoves=new HashMap();	Button nG,learn,rs,help;	boolean humanTurn;	Random r=new Random();	int lastX;	int lastY;	public void start()	{		setLayout(null);		addMouseListener(this);		nG=new Button("NEW GAME");		rs=new Button("CLEAR MEMORY");		learn=new Button("LEARN");		help=new Button("HELP");		nG.setBounds(10,1,80,20);		nG.addActionListener(this);		help.setBounds(10,328,280,20);		help.addActionListener(this);		rs.setBounds(100,1,105,20);		rs.addActionListener(this);		learn.setBounds(215,1,75,20);		learn.addActionListener(this);		add(nG);		add(rs);		add(help);		add(learn);	}	public void init()	{		humanTurn=true;		lastX=-1;lastY=-1;		board=new String[3][3];		for(int cv=0;cv<9;cv++)		{			board[cv/3][cv%3]=" ";		}	}	public void learn()	{		for(int cv=0;cv<100;cv++)		{			for(int cv2=0;cv2<50;cv2++)			{				int x;				int y;				boolean isValid=false;				while(!isValid)				{					x=r.nextInt(3);					y=r.nextInt(3);					lastX=x;					lastY=y;					isValid=move(x,y,"X");				}			}		}		init();		repaint();	}	public void paint(Graphics gr)	{		Graphics g=gr;		for(int cv=0;cv<4;cv++)		{			g.drawLine(0,100*cv+24,300,100*cv+24);			g.drawLine(100*cv,24,100*cv,324);		}		for(int cv=0;cv<3;cv++)		{			for(int cv2=0;cv2<3;cv2++)			{				g.drawString(board[cv][cv2],100*cv+50,100*cv2+75);			}		}		gr=g;	}	public void mouseEntered(MouseEvent e){}	public void mouseExited(MouseEvent e){}	public void mousePressed(MouseEvent e)	{		if(humanTurn)		{			if(e.getX()>299||e.getX()<0||e.getY()<25||e.getY()>323)			return;			int x=e.getX()/100;			int y=(e.getY()-24)/100;			lastX=x;			lastY=y;			move(x,y,"X");		}	}	public void mouseReleased(MouseEvent e){}	public void mouseClicked(MouseEvent e){}	public boolean move(int x,int y,String player)	{		if(board[x][y].equals(" "))		board[x][y]=player;		else return false;		humanTurn=false;		repaint();		String win=checkWIN(player);		if(win.equals("yes"))		{			showStatus(player+" wins!");			return true;		}		if(win.equals("tie"))		{			showStatus("DRAW");			return true;		}		if(player.equals("X"))		computerMove();		return true;	}	public void computerMove()	{		String tempBoard[][]=tempBoard();		int cv=0;		while(cv<9)		{			if(board[cv%3][cv/3].equals(" "))			{				tempBoard[cv%3][cv/3]="O";				int y=cv/3;				int x=cv%3;				if(!badMoves.containsKey(getString(tempBoard)))				{										move(x,y,"O");					humanTurn=true;					repaint();					return;				}			}			cv++;			tempBoard=tempBoard();		}		String[][] b=tempBoard();		b[lastX][lastY]=" ";		badMoves.put(getString(b),"bad");		cv=0;		int x=r.nextInt(3);		int y=r.nextInt(3);		boolean success=move(x,y,"O");		while(!success)		{			x=r.nextInt(3);			y=r.nextInt(3);			success=move(x,y,"O");		}		humanTurn=true;		repaint();	}	public String checkWIN(String p)	{		if((board[0][0].equals(board[0][1])&&board[0][0].equals(board[0][2])&&board[0][0].equals(p))||(board[1][0].equals(board[1][1])&&board[1][0].equals(board[1][2])&&board[1][0].equals(p))||(board[2][0].equals(board[2][1])&&board[2][0].equals(board[2][2])&&board[2][0].equals(p))||(board[0][0].equals(board[1][0])&&board[0][0].equals(board[2][0])&&board[0][0].equals(p))||(board[0][1].equals(board[1][1])&&board[0][1].equals(board[2][1])&&board[0][1].equals(p))||(board[0][2].equals(board[1][2])&&board[0][2].equals(board[2][2])&&board[0][2].equals(p))||(board[0][0].equals(board[1][1])&&board[0][0].equals(board[2][2])&&board[0][0].equals(p))||(board[0][2].equals(board[1][1])&&board[0][2].equals(board[2][0])&&board[2][0].equals(p)))		{			if(p.equals("X"))			{				test=board;				board[lastX][lastY]=" ";				badMoves.put(getString(board),"bad");			}			init();			repaint();			return "yes";		}		for(int cv=0;cv<9;cv++)		{			if(board[cv%3][cv/3].equals(" "))			return "no";		}		init();		repaint();		return "tie";	}	public String getString(String b[][])	{		return b[0][0]+b[0][1]+b[0][2]+b[1][0]+b[1][1]+b[1][2]+b[2][0]+b[2][1]+b[2][2];	}	public String[][] tempBoard()	{		String[][] tB=new String[3][3];		for(int cv=0;cv<9;cv++)		{			tB[cv/3][cv%3]=board[cv/3][cv%3];		}		return tB;	}	public void actionPerformed(ActionEvent e)	{		if(e.getSource()==rs)		{			badMoves=new HashMap();			goodMoves=new HashMap();			tieMoves=new HashMap();			init();			repaint();		}		if(e.getSource()==nG)		{			init();			repaint();		}		if(e.getSource()==learn)		{			learn();		}		if(e.getSource()==help)		{			JOptionPane.showMessageDialog(null,"You are X.\nThe computer is O.\n\nGet 3-in-a-row to win.\n\nClick NEW GAME to create a new game.\nClick LEARN to make the computer smarter.\n(If you click it 5 or more times then it will be VIRTUALLY undefeatable)\nClick CLEAR MEMORY to make the computer stupid again.","HELP",JOptionPane.INFORMATION_MESSAGE);		}	}}

This topic is closed to new replies.

Advertisement