All pastes #57391 Raw Edit

tictacmouse

public java v1 · immutable
#57391 ·published 2006-05-20 23:11 UTC
rendered paste body
// The "TicTacToe" class.import java.applet.*;import java.awt.*;import java.awt.event.*;public class TicTacToe extends Applet{    int numCase;    int[] [] place;    place = new int [3] [3];    // Place instance variables here    public boolean mouseDown (Event evt, int x, int y)    {        if (x < (getSize ().width / 3) && y < (getSize ().height / 3)) //1,1        {            numCase = 1;        }        if (x > (getSize ().width / 3) && x < (getSize ().width * 2 / 3) && y < (getSize ().height / 3)) //1,2        {            numCase = 2;        }        if (x > (getSize ().width * 2 / 3) && y < (getSize ().height / 3)) //1,3        {            numCase = 3;        }        if (x < (getSize ().width / 3) && y > (getSize ().height / 3) && y < (getSize ().height * 2 / 3)) //2,1        {            numCase = 4;        }        if (x > (getSize ().width / 3) && x < (getSize ().width * 2 / 3) && y > (getSize ().height / 3) && y < (getSize ().height * 2 / 3)) //2,2        {            numCase = 5;        }        if (x > (getSize ().width * 2 / 3) && y > (getSize ().height / 3) && y < (getSize ().height * 2 / 3)) //2,3        {            numCase = 6;        }        if (x < (getSize ().width / 3) && y > (getSize ().height * 2 / 3)) //3,1        {            numCase = 7;        }        if (x > (getSize ().width / 3) && x < (getSize ().width * 2 / 3) && y > (getSize ().height * 2 / 3)) //3,2        {            numCase = 8;        }        if (x > (getSize ().width * 2 / 3) && y > (getSize ().height * 2 / 3)) //3,3        {            numCase = 9;        }        return true;    }    public void init ()    {    }    public void paint (Graphics g)    {        g.drawLine (getSize ().width / 3, 0, getSize ().width / 3, getSize ().height);        g.drawLine (getSize ().width * 2 / 3, 0, getSize ().width * 2 / 3, getSize ().height);        g.drawLine (0, getSize ().height / 3, getSize ().width, getSize ().height / 3);        g.drawLine (0, getSize ().height * 2 / 3, getSize ().width, getSize ().height * 2 / 3);        for (int turn = 0 ; turn < 9 ; turn++)        {            switch (numCase)            {                case 1: //1,1                    if ((turn % 2) == 1)                    {                        g.drawLine (0, 0, getSize ().width / 3, getSize ().height / 3);                        g.drawLine (0, getSize ().height / 3, getSize ().width / 3, 0);                        place [1] [1] = 1;                    }                    else                    {                        g.drawOval (0, 0, getSize ().width / 3, getSize ().height / 3);                    }                    break;                case 2: //1,2                    if ((turn % 2) == 1)                    {                        g.drawLine (getSize ().width / 3, 0, getSize ().width * 2 / 3, getSize ().height / 3);                        g.drawLine (getSize ().width / 3, getSize ().height / 3, getSize ().width * 2 / 3, 0);                    }                    else                    {                        g.drawOval (getSize ().width / 3, 0, getSize ().width / 3, getSize ().height / 3);                    }                    break;                case 3: //1,3                    if ((turn % 2) == 1)                    {                        g.drawLine (getSize ().width * 2 / 3, 0, getSize ().width, getSize ().height / 3);                        g.drawLine (getSize ().width * 2 / 3, getSize ().height / 3, getSize ().width, 0);                    }                    else                    {                        g.drawOval (getSize ().width * 2 / 3, 0, getSize ().width / 3, getSize ().height / 3);                    }                    break;                case 4: //2,1                    if ((turn % 2) == 1)                    {                        g.drawLine (0, getSize ().height / 3, getSize ().width / 3, getSize ().height * 2 / 3);                        g.drawLine (0, getSize ().height * 2 / 3, getSize ().width / 3, getSize ().height / 3);                    }                    else                    {                        g.drawOval (0, getSize ().height / 3, getSize ().width / 3, getSize ().height / 3);                    }                    break;                case 5: //2,2                    if ((turn % 2) == 1)                    {                        g.drawLine (getSize ().width / 3, getSize ().height / 3, getSize ().width * 2 / 3, getSize ().height * 2 / 3);                        g.drawLine (getSize ().width / 3, getSize ().height * 2 / 3, getSize ().width * 2 / 3, getSize ().height / 3);                    }                    else                    {                        g.drawOval (getSize ().width / 3, getSize ().height / 3, getSize ().width / 3, getSize ().height / 3);                    }                    break;                case 6: //2,3                    if ((turn % 2) == 1)                    {                        g.drawLine (getSize ().width * 2 / 3, getSize ().height / 3, getSize ().width, getSize ().height * 2 / 3);                        g.drawLine (getSize ().width * 2 / 3, getSize ().height * 2 / 3, getSize ().width, getSize ().height / 3);                    }                    else                    {                        g.drawOval (getSize ().width * 2 / 3, getSize ().height / 3, getSize ().width / 3, getSize ().height / 3);                    }                    break;                case 7: //3,1                    if ((turn % 2) == 1)                    {                        g.drawLine (0, getSize ().height * 2 / 3, getSize ().width / 3, getSize ().height);                        g.drawLine (0, getSize ().height, getSize ().width / 3, getSize ().height * 2 / 3);                    }                    else                    {                        g.drawOval (0, getSize ().height * 2 / 3, getSize ().width / 3, getSize ().height / 3);                    }                    break;                case 8: //3,2                    if ((turn % 2) == 1)                    {                        g.drawLine (getSize ().width / 3, getSize ().height * 2 / 3, getSize ().width * 2 / 3, getSize ().height);                        g.drawLine (getSize ().width / 3, getSize ().height, getSize ().width * 2 / 3, getSize ().height * 2 / 3);                    }                    else                    {                        g.drawOval (getSize ().width / 3, getSize ().height * 2 / 3, getSize ().width / 3, getSize ().height / 3);                    }                    break;                case 9: //3,3                    if ((turn % 2) == 1)                    {                        g.drawLine (getSize ().width * 2 / 3, getSize ().height * 2 / 3, getSize ().width, getSize ().height);                        g.drawLine (getSize ().width * 2 / 3, getSize ().height, getSize ().width, getSize ().height * 2 / 3);                    }                    else                    {                        g.drawOval (getSize ().width * 2 / 3, getSize ().height * 2 / 3, getSize ().width / 3, getSize ().height / 3);                    }                    break;                default:                    break;            }            System.out.println (place [1] [1]);        }    }}