import java.awt.*; import javax.swing.*; import java.awt.event.*; import java.awt.image.*; /** * Let’s write the flow of the game together: * 1. In initialize: randomly select user or computer to go first, * 2. In update tell user their turn, or computers turn * 3a. If computers turn, delay, have computer make move * 3b. If users turn, listen for mouse and when clicked * if move is not valid, tell users they are blind and go to 3b * if move is valid, make the move * 4. Draw Board - all in update * 5. Determine status of board * if status is undecided, flip whose turn and go to 2 * otherwise tell the user its over and result * @author (your name) * @version (a version number or a date) */ public class TicTacToe extends BorlandBase { Grid myGrid=new Grid(); Computer myComputer=new Computer(); int turn=findRandom(1,2); public void initialize() { //setWidth(300); setWidth(800); setHeight(800); setAnimation(true); setBackgroundColor(Color.blue); setDelay(500); } //when someone presses the mouse button // I draw on temporary layer // I call drawScreen to have it show up. // I pause 2000 ms // Then I get temporary again and draw again public void mousePressed(MouseEvent e) { //an example of passing Graphics g=getTempG(); g.fillOval(50,50,50,50); drawScreen(); pause(500); stop(); g.setColor(Color.black); g.fillOval(150,150,50,50); drawScreen(); pause(500); } public void paintScreen (Graphics g) { //this happens once in the beginning. any drawing you want permanent (like grid do here) } public void update(Graphics g) { //check if game is still in play //if its comp turn have computer make a move //if its users turn, tell them //if game is over, say so (and stop animation) myGrid.drawGrid(g); if (turn==2) { g.drawString ("Computer's turn",200,150); drawScreen(); stop(); pause(1500); myComputer.makeMove(myGrid); turn=1; } if (turn==1) g.drawString ("User's turn",200,150); } public static void main(String[] args) { try{ BorlandBase.main((BorlandBase)(new Object() { }.getClass().getEnclosingClass().newInstance())); } catch (Exception e){e.printStackTrace();} } }