Tic Tac Toe is very simple game. It need only very simple AI to develop. So I'm giving you very simple Java code which the CPU player can find the empty cell and play there. There is no other skills on CPU player.
The only characteristics of CPU player is to find the empty cells and select a cell in random to play there.
Read Advanced CPU Player: http://terminalcoders.blogspot.com/2014/08/tic-tac-toe-ai-fully-functional.html
The only characteristics of CPU player is to find the empty cells and select a cell in random to play there.
Read Advanced CPU Player: http://terminalcoders.blogspot.com/2014/08/tic-tac-toe-ai-fully-functional.html
/* * * * ARTIFICIAL INTELLIGENT --- X --- CODED BY AJITH KP BASICS * * */ import java.awt.BorderLayout; import java.awt.Color; import java.awt.Font; import java.awt.GridLayout; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; import java.util.Random; import javax.swing.BorderFactory; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JTextField; public class TicTacToe extends JFrame { JPanel l = new JPanel(new GridLayout(1, 1, 0, 0)); JPanel b = new JPanel(new GridLayout(3, 3, 5, 5)); JLabel lbl = new JLabel("Coded By Ajith Kp"); JTextField flds[] = new JTextField[9]; boolean pl = false; TextHandle hld = new TextHandle(); TicTacToe() { super("TicTacToe --- AJITH KP"); setLayout(new BorderLayout()); for(int i=0;i<9;i++) { flds[i] = new JTextField(); flds[i].addMouseListener(hld); flds[i].setForeground(Color.BLACK); flds[i].setHorizontalAlignment(JLabel.CENTER); flds[i].setBorder(BorderFactory.createMatteBorder(0, 0, 0, 0, Color.red)); flds[i].setBackground(Color.CYAN); flds[i].setFont(new Font("Times New Roma", Font.BOLD, 25)); flds[i].setEditable(false); b.add(flds[i]); } lbl.setForeground(Color.BLUE); lbl.setFont(new Font("Times New Roman", Font.BOLD, 25)); lbl.setBorder(BorderFactory.createMatteBorder(3, 3, 3, 3, Color.green)); lbl.setHorizontalAlignment(JLabel.CENTER); l.add(lbl); add(l, BorderLayout.NORTH); add(b, BorderLayout.CENTER); } public class TextHandle extends MouseAdapter { @Override public void mouseClicked(MouseEvent e) { if(pl==false) { JTextField fld = (JTextField) e.getSource(); if(fld.getText().equals("")) { fld.setForeground(Color.red); fld.setBorder(BorderFactory.createMatteBorder(3, 3, 3, 3, Color.red)); fld.setText("X"); char c = finish(); if(c=='U') { lbl.setText("You have won"); over(); return; } else if(c=='O') { lbl.setText("Game Over. No one won"); over(); return; } pl = true; comp(); } } } } public void comp() { if(pl==true) { int n = bestClic(); flds[n].setForeground(Color.BLUE); flds[n].setBorder(BorderFactory.createMatteBorder(3, 3, 3, 3, Color.BLUE)); flds[n].setText("O"); char c = finish(); if(c=='C') { lbl.setText("Computer has won"); over(); return; } else if(c=='O') { lbl.setText("Game Over. No one won."); over(); return; } pl = false; } } public int bestClic() { int num = 0; int free[] = free(); Random ra = new Random(); int r = ra.nextInt(free.length); if(flds[4].getText().equals("")) { num = 4; } else { num = free[r]; } return num; } public int[] free() { int cnt = 0; for(int i=0;i<9;i++) { if(flds[i].getText().equals("")) { cnt++; } } int num[] = new int[cnt]; cnt = 0; for(int i=0;i<9;i++) { if(flds[i].getText().equals("")) { num[cnt] = i; cnt++; } } return num; } public char finish() { char win = 'N'; int c1, c2, c3, r1, r2, r3, x1, x2; c1 = c2 = c3 = r1 = r2 = r3 = x1 = x2 = 0; for(int i=0;i<9;i++) { if(i%3==0) { if(flds[i].getText().equals("X")) { c1++; } } if(i%3==1) { if(flds[i].getText().equals("X")) { c2++; } } if(i%3==2) { if(flds[i].getText().equals("X")) { c3++; } } if(i<3) { if(flds[i].getText().equals("X")) { r1++; } } else if(i<6) { if(flds[i].getText().equals("X")) { r2++; } } else if(i<9) { if(flds[i].getText().equals("X")) { r3++; } } if(i==0||i==4||i==8) { if(flds[i].getText().equals("X")) { x1++; } } if(i==2||i==4||i==6) { if(flds[i].getText().equals("X")) { x2++; } } } if(c1 == 3|| c2 == 3|| c3 == 3 || r1 == 3 || r2 == 3 || r3 == 3 || x1 == 3 || x2 == 3) { return 'U'; } c1 = c2 = c3 = r1 = r2 = r3 = x1 = x2 = 0; for(int i=0;i<9;i++) { if(i%3==0) { if(flds[i].getText().equals("O")) { c1++; } } if(i%3==1) { if(flds[i].getText().equals("O")) { c2++; } } if(i%3==2) { if(flds[i].getText().equals("O")) { c3++; } } if(i<3) { if(flds[i].getText().equals("O")) { r1++; } } else if(i<6) { if(flds[i].getText().equals("O")) { r2++; } } else if(i<9) { if(flds[i].getText().equals("O")) { r3++; } } if(i==0||i==4||i==8) { if(flds[i].getText().equals("O")) { x1++; } } if(i==2||i==4||i==6) { if(flds[i].getText().equals("O")) { x2++; } } } if(c1 == 3|| c2 == 3|| c3 == 3 || r1 == 3 || r2 == 3 || r3 == 3 || x1 == 3 || x2 == 3) { return 'C'; } int c = 0; for(int i=0;i<9;i++) { if(flds[i].getText().equals("")) { c++; } } if(c==0) { return 'O'; } return win; } public void over() { for(int i=0;i<9;i++) { flds[i].removeMouseListener(hld); } } public static void main(String[] args) { TicTacToe t = new TicTacToe(); t.setVisible(true); t.setSize(350, 370); t.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } }