⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 tictacgrid.java

📁 我们学校的TicTacToe的java作业描述
💻 JAVA
字号:
/*  Written by: YG  First written: 15/10/06  Last modified: 15/10/06*/import sheffield.*;public class TicTacGrid {    // constants    private static final int MARGIN = 20;	// margin around the grid    private static final int NUM_STEPS = 30;	// steps for drawing circles    private static final int GRID_SIZE = 300;	// active grid size    public static final int EMPTY = 0;		// integer symbol for empty box    public static final int CROSS = 1;		// integer symbol for cross    public static final int CIRCLE = 2;		// integer symbol for circle    // instance fields    private EasyGraphics grid;		// graphics window    private int dimension;		// grid dimension    private int playersChoice;		// either CROSS or CIRCLE    private TicTacReference reference;	// internal reference    private int crossLength;		// the cross sequence length    private int circleLength;		// the circle sequence length    // constructor    public TicTacGrid(int d, int l, String p) {	// d must be between 3 and 9	if ((2<d) && (d<10)) {	    dimension = d;	}	else {	    System.out.print("Warning: ");	    System.out.println("the grid dimension must be between 3 and 9.");	    System.exit(0);	}	// p must be either 'x' or 'o'	if (p.equals("x")) {	    playersChoice = CROSS;	}	else if (p.equals("o")) {	    playersChoice = CIRCLE;	}	else {	    System.out.print("Warning: ");	    System.out.println("the mark must be either \'x\' or \'o\'.");	    System.exit(0);	}	// draw a grid	int gridSize = GRID_SIZE+(MARGIN*2);	double boxSize = GRID_SIZE/dimension;	grid = new EasyGraphics(gridSize, gridSize, MARGIN, MARGIN);	for (int x=1; x<dimension; x++) {	    double tmp = boxSize*x;	    grid.moveTo(0, tmp);	    grid.lineTo(GRID_SIZE, tmp);	    grid.moveTo(tmp, 0);	    grid.lineTo(tmp, GRID_SIZE);	}	// create a reference	reference = new TicTacReference(dimension, l, EMPTY);    }    // move by the player    public boolean playerDoes(String s) {	boolean flag = true;	// test the string length	TicTacIndex a = new TicTacIndex(dimension);	flag = a.testStringLength(s);	if (flag==false) {	    System.out.println("\t\t\"" + s + "\" is not valid");	}	// convert the string to indices	else {	    flag = a.stringToIndex(s);	    if (flag==false) {		System.out.println("\t\t\"" + s + "\" is not valid");	    }	    // test if the box(x, y) is empty	    else {		int x = a.getX();		int y = a.getY();		flag = reference.isEmpty(x, y);		if (flag==false) {		    System.out.println("\t\t\"" + s + "\" is not empty");		}		// place a mark on the grid		else if (playersChoice==CROSS) {		    setCross(x, y);		    reference.setBox(x, y, CROSS);		    crossLength = reference.sequenceLength(x, y, CROSS);		}		else {		    setCircle(x, y);		    reference.setBox(x, y, CIRCLE);		    circleLength = reference.sequenceLength(x, y, CIRCLE);		}	    }	}	// return the flag	return(flag);    }    // move by the algorithm    public String algorithmDoes() {	// choose a move	TicTacIndex b = reference.chooseAMove();	// move by the algorithm	int x = b.getX();	int y = b.getY();	// place a mark on the grid	if (playersChoice==CROSS) {	    setCircle(x, y);	    reference.setBox(x, y, CIRCLE);	    circleLength = reference.sequenceLength(x, y, CIRCLE);	}	else {	    setCross(x, y);	    reference.setBox(x, y, CROSS);	    crossLength = reference.sequenceLength(x, y, CROSS);	}	// return the move	return(b.getBoxPosition());    }    // fill a cross in a box    private void setCross(int x, int y) {	double boxSize = GRID_SIZE/dimension;	// draw a cross	grid.moveTo(boxSize*(x+0.3), boxSize*(y+0.3));	grid.lineTo(boxSize*(x+0.7), boxSize*(y+0.7));	grid.moveTo(boxSize*(x+0.3), boxSize*(y+0.7));	grid.lineTo(boxSize*(x+0.7), boxSize*(y+0.3));    }    // fill a circle in a box    private void setCircle(int x, int y) {	double boxSize = GRID_SIZE/dimension;	double radius = boxSize/5;	double theta = 2.0*Math.PI/(NUM_STEPS-1);	// draw a circle	grid.moveTo(boxSize*(x+0.5)+radius, boxSize*(y+0.5));	for (int i=1; i<NUM_STEPS; i++) {	    grid.lineTo(boxSize*(x+0.5)+radius*Math.cos(theta*i),			boxSize*(y+0.5)+radius*Math.sin(theta*i));	}    }    // the game continues while (1) the sequence is shorter than the grid    // dimension and (2) there are empty boxes    public int gameOver() {	int winner = -1;	// cross wins	if (crossLength>=dimension) {	    winner = CROSS;	}	// circle wins	else if (circleLength>=dimension) {	    winner = CIRCLE;	}	// no box left empty	else if (reference.getNumberOfEmptyBoxes()==0) {	    winner = EMPTY;	}	return(winner);    }}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -