How the computer is not "Random"
I want the player can not win!
Player only draw!
Thank..:yawn:
How the computer is not "Random"
I want the player can not win!
Player only draw!
Thank..:yawn:
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
import javax.swing.*;
import java.util.Random;
public class TicTacToe extends JFrame {
private Board board;
static final char BLANK=' ', O='O', X='X';
private char position[]={ // Board position (BLANK, O, or X)
BLANK, BLANK, BLANK,
BLANK, BLANK, BLANK,
BLANK, BLANK, BLANK};
private int wins=0, losses=0, draws=0; // game count by user
// Start the game
public static void main(String args[]) {
new TicTacToe();
}
// Constructor of TicTacToe: Initialize settings
public TicTacToe() {
super("Welcome Tic Tac Toe!");
add(board=new Board(), BorderLayout.CENTER);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(500, 500);
setVisible(true);
}
// Board is what actually plays and displays the game
private class Board extends JPanel implements MouseListener {
// Constructor of Board
public Board() {
addMouseListener(this);
}
// Redraw the board
public void paintComponent(Graphics g) {
super.paintComponent(g);
int w=getWidth();
int h=getHeight();
Graphics2D g2d = (Graphics2D) g;
// Draw the grid
g2d.setPaint(Color.black);
g2d.fill(new Rectangle2D.Double(0, 0, w, h));
g2d.setPaint(Color.yellow);
g2d.setStroke(new BasicStroke(10));
g2d.draw(new Line2D.Double(0, h/3, w, h/3));
g2d.draw(new Line2D.Double(0, h*2/3, w, h*2/3));
g2d.draw(new Line2D.Double(w/3, 0, w/3, h));
g2d.draw(new Line2D.Double(w*2/3, 0, w*2/3, h));
// Draw the Os and Xs
for (int i=0; i<9; ++i) {
double xpos=(i%3+0.5)*w/3.0;
double ypos=(i/3+0.5)*h/3.0;
double xr=w/10.0; //
double yr=h/10.0;
if (position[i]==O) {
g2d.setPaint(Color.red);
g2d.draw(new Ellipse2D.Double(xpos-xr, ypos-yr, xr*2, yr*2));
}
else if (position[i]==X) {
g2d.setPaint(Color.green);
g2d.draw(new Line2D.Double(xpos-xr, ypos-yr, xpos+xr, ypos+yr));
g2d.draw(new Line2D.Double(xpos-xr, ypos+yr, xpos+xr, ypos-yr));
}
}
}
// Draw an O where the mouse is clicked
public void mouseClicked(MouseEvent e) {
int xpos=e.getX()*3/getWidth();
int ypos=e.getY()*3/getHeight();
int pos=xpos+3*ypos;
if (pos>=0 && pos<9 && position[pos]==BLANK) {
position[pos]=O;
repaint();
putX(); // computer plays
repaint();
}
}
// Ignore other mouse events
public void mousePressed(MouseEvent e) {}
public void mouseReleased(MouseEvent e) {}
public void mouseEntered(MouseEvent e) {}
public void mouseExited(MouseEvent e) {}
// Computer plays X
public void putX() {
// Check if game is over
if (won(O))
newGame(O);
else if (isDraw())
newGame(BLANK);
// Play X, possibly ending the game
else {
nextMove();
if (won(X))
newGame(X);
else if (isDraw())
newGame(BLANK);
}
}
// Play X in one of the empty spot
public void nextMove() {
int r;
do {
Random random=new Random();
r=random.nextInt(9); // move randomly
} while (position[r]!=BLANK);
position[r]=X;
}
// Return true if player has won
public boolean won(char player) {
if((position[0]==position[1]&&position[1]==position[2]&&position[0]==player)||
(position[3]==position[4]&&position[4]==position[5]&&position[3]==player)||
(position[6]==position[7]&&position[7]==position[8]&&position[6]==player)||
(position[0]==position[3]&&position[3]==position[6]&&position[0]==player)||
(position[1]==position[4]&&position[4]==position[7]&&position[1]==player)||
(position[2]==position[5]&&position[5]==position[8]&&position[2]==player)||
(position[0]==position[4]&&position[4]==position[8]&&position[0]==player)||
(position[2]==position[4]&&position[4]==position[6]&&position[2]==player)){
return true;
}
return false;
}
// Are all 9 spots filled? Return true if yes
public boolean isDraw() {
if(position[0]!=BLANK&&
position[1]!=BLANK&&
position[2]!=BLANK&&
position[3]!=BLANK&&
position[4]!=BLANK&&
position[5]!=BLANK&&
position[6]!=BLANK&&
position[7]!=BLANK&&
position[8]!=BLANK)
return true;
else
return false;
}
// Start a new game
public void newGame(char winner) {
repaint();
// Announce result of last game. Ask user to play again.
String result;
if (winner==O) {
++wins;
result = "You Win!";
}
else if (winner==X) {
++losses;
result = "I Win!";
}
else {
result = "Tie";
++draws;
}
if (JOptionPane.showConfirmDialog(null,
"You have "+wins+ " wins, "+losses+" losses, "+draws+" draws\n"
+"Play again?", result, JOptionPane.YES_NO_OPTION)
!=JOptionPane.YES_OPTION) {
System.exit(0);
}
// Clear the board to start a new game
for (int j=0; j<9; ++j)
position[j]=BLANK;
// Computer starts first every other game
if ((wins+losses+draws)%2 == 1)
nextMove();
}
} // end inner class Board
} // end class TicTacToe
You are never going to get that kind of help here. Post specific parts of your code that doesn't work and describe what you have done so far.
why
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.