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

📄 tictacreference.java

📁 我们学校的TicTacToe的java作业描述
💻 JAVA
字号:
/*  Written by: YG  First written: 15/10/06  Last modified: 15/10/06*/import sheffield.*;public class TicTacReference {    // constants    private static final int BEGINNER = 1;	// beginner level    private static final int MASTER = 2;	// master level    // instance fields    private int dimension;	// grid dimension    private int playlevel;	// play level    private int empty;		// integer symbol for empty box    private int[][] box;	// internal references    private int emptyBoxes;	// the number of boxes left empty    // constructor    public TicTacReference(int d, int l, int symbol) {	dimension = d;	empty = symbol;	// play level must be either 1 or 2	if (l==1) {	    playlevel = BEGINNER;	}	else if (l==2) {	    playlevel = MASTER;	}	else {	    System.out.print("Warning: ");	    System.out.println("the play level must be either 1 or 2.");	    System.exit(0);	}	// initialise the reference	box = new int[dimension][dimension];	for (int x=0; x<dimension; x++) {	    for (int y=0; y<dimension; y++) {		box[x][y] = empty;	    }	}	// initialise the number of empty boxes	emptyBoxes = dimension*dimension;    }    // choose a move by the algorithm    public TicTacIndex chooseAMove() {	TicTacIndex b;	// beginner level	if (playlevel == BEGINNER) {	    b = ramdomChoice();	}	// master level	else {	    b = ramdomChoice();	}	return(b);    }    // make a random choice from empty boxes    private TicTacIndex ramdomChoice() {	// choose indices of one empty box	int i = 0;	int j = 0;	int count = 0;	int choice = (int)(Math.random()*emptyBoxes);	for (int x=0; x<dimension; x++) {	    for (int y=0; y<dimension; y++) {		// test if the box(x, y) is empty		if (isEmpty(x, y)) {		    if (count==choice) {			i = x;			j = y;		    }		    count++;		}	    }	}	// return the indices	TicTacIndex b = new TicTacIndex(dimension);	b.indexToString(i, j);	return(b);    }    // test if the box(x, y) is empty    public boolean isEmpty(int x, int y) {	boolean flag = false;	if (box[x][y]==empty) {	    flag = true;	}	return(flag);    }    // set a symbol to box(x, y)    public void setBox(int x, int y, int symbol) {	box[x][y] = symbol;	updateNumberOfEmptyBoxes();    }    // return a symbol for box(x, y)    public int getBox(int x, int y) {	return(box[x][y]);    }    // update the number of empty boxes    public void updateNumberOfEmptyBoxes() {	emptyBoxes--;    }    // return the number of empty boxes    public int getNumberOfEmptyBoxes() {	return(emptyBoxes);    }    // find the sequence length for a symbol around box(x, y)    public int sequenceLength(int x, int y, int symbol) {	int maxSequence = 0;	// test left to right	int count = 0;	int i = x;	while ((i>=0) && (box[i][y]==symbol)) {	    count++;	    i--;	}	i = x+1;	while ((i<dimension) && (box[i][y]==symbol)) {	    count++;	    i++;	}	if (maxSequence < count) {	    maxSequence = count;	}	// test up to down	count = 0;	int j = y;	while ((j>=0) && (box[x][j]==symbol)) {	    count++;	    j--;	}	j = y+1;	while ((j<dimension) && (box[x][j]==symbol)) {	    count++;	    j++;	}	if (maxSequence < count) {	    maxSequence = count;	}	// test lower left to upper right	count = 0;	i = x;	j = y;	while ((i>=0) && (j>=0) && (box[i][j]==symbol)) {	    count++;	    i--;	    j--;	}	i = x+1;	j = y+1;	while ((i<dimension) && (j<dimension) && (box[i][j]==symbol)) {	    count++;	    i++;	    j++;	}	if (maxSequence < count) {	    maxSequence = count;	}	// lower left to upper right	count = 0;	i = x;	j = y;	while ((i>=0) && (j<dimension) && (box[i][j]==symbol)) {	    count++;	    i--;	    j++;	}	i = x+1;	j = y-1;	while ((i<dimension) && (j>=0) && (box[i][j]==symbol)) {	    count++;	    i++;	    j--;	}	if (maxSequence < count) {	    maxSequence = count;	}	// return the length of sequence	return(maxSequence);    }    // main method for testing the class    public static void main(String[] args) {	// CODE MISSING    }}

⌨️ 快捷键说明

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