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

📄 tictacindex.java

📁 我们学校的TicTacToe的java作业描述
💻 JAVA
字号:
/*  Written by: YG  First written: 15/10/06  Last modified: 15/10/06*/import sheffield.*;public class TicTacIndex {    // constants    private static final int STRING_LENGTH = 2;	// string length    // instance fields    private String boxPosition;	// string representation of a box position    private int x;		// x-axis index in the grid    private int y;		// y-axis index in the grid    private int maxIndex;	// maximum index allowed    // constructor    public TicTacIndex(int d) {	boxPosition = null;	x = 0;	y = 0;	maxIndex = d;    }    // test the length of the string    public boolean testStringLength(String s) {	boolean flag = true;	if (s.length()!=STRING_LENGTH) {	    flag = false;	}	return(flag);    }    // convert the string representation to integer indices    public boolean stringToIndex(String s) {	boxPosition = s;	// convert the string	x = boxPosition.charAt(0) - (int)'1';	y = boxPosition.charAt(1) - (int)'a';	// test if indices (x, y) are vaild	boolean flag = testIndex(x, y);	return(flag);    }    // test indices    private boolean testIndex(int i, int j) {	boolean flag = true;	if ((i<0) || (maxIndex<=i) || (j<0) || (maxIndex<=j)) {	    flag = false;	}	return(flag);    }    // convert integer indices to a string representation    public void indexToString(int i, int j) {	x = i;	y = j;	// convert to a string	char[] c = new char[2];	c[0] = (char)((int)'1' + x);	c[1] = (char)((int)'a' + y);	boxPosition = String.copyValueOf(c);    }    // return the x-axis index in the grid    public int getX() {	return(x);    }    // return the y-axis index in the grid    public int getY() {	return(y);    }    // return the string representation of a box position    public String getBoxPosition() {	return(boxPosition);    }    // main method for testing the class    public static void main(String[] args) {	EasyReader keyboard = new EasyReader();	// read the grid dimension	System.out.println("The grid dimension must be between 3 and 9.");	int d = keyboard.readInt("  Enter the dimension: ");	// read a string from the keyboard, then create an object	String str = keyboard.readString("\n  Enter a string: ");	TicTacIndex a = new TicTacIndex(d);	// test the length of the string	boolean flag = a.testStringLength(str);	if (flag==false) {	    System.out.println("\t\"" + str + "\" is not valid");	}	// convert a string representation to indices	else {	    flag = a.stringToIndex(str);	    if (flag==false) {		System.out.println("\t\"" + str + "\" is not valid");	    }	    else {		System.out.print("\t(i, j) = ");		System.out.println("(" + a.getX() + ", " + a.getY() + ")");	    }	}	// read indices from the keyboard, then create an object	int i = keyboard.readInt("\n  Enter the first index: ");	int j = keyboard.readInt("  Enter the second index: ");	TicTacIndex b = new TicTacIndex(d);	// test if indices are valid	flag = b.testIndex(i, j);	if (flag==false) {	    System.out.println("\t(" + i + ", " + j + ") is not valid");	}	// convert indices to a string representation	else {	    b.indexToString(i, j);	    System.out.println("\tstring = \"" + b.getBoxPosition() + "\"");	}    }}

⌨️ 快捷键说明

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