Anonymous Anonymous - 1 month ago
510 0

No description

Java

TicTacToe

package interfaces;
import java.util.ArrayList;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.io.FileReader;
import java.util.Scanner;

public class TicTacToe implements GameInterface{
	
	ArrayList<UndoRedo> undoMove = new ArrayList<UndoRedo>();
	ArrayList<UndoRedo> redoMove = new ArrayList<UndoRedo>();
	
	static StringBuilder board = new StringBuilder("         ");
	char player1 = 'X';
	
	private int getIndex(int x, int y){
		int counter = 0;
		for (int i = 0; i < 3; i++) {
			for (int j = 0; j < 3; j++) {
				if (x == i && y == j) {
					return counter;
				}
				counter++;
			}
		}
		return 0;
	}
	
	public char getCell(int x, int y){
		return board.charAt(getIndex(x, y));
	}
	
	public boolean setCell(char c, int x, int y){
		if (isOccupied(x, y)) {
			return false;
		}
		else {
			board.setCharAt(getIndex(x, y), c);
			return true;
		}
	}
	
	public boolean isOccupied(int x, int y){
		if (getCell(x,y) == 'X' || getCell(x,y) == 'O') {
			return true;
		}
		else {
			return false;
		}
	}
	
	public char getCurrentPlayer(){
		int counterX = 0;
		int counterO = 0;
		char player = ' ';
		for(int i = 0; i < 9; i++){
			if(board.charAt(i) == 'X'){
				counterX += 1;
			}
			if(board.charAt(i) == 'O'){
				counterO += 1;
			}
		}
		if(counterX > counterO){
			player = 'O';
		}
		else{
			player = 'X';
		}
		return player;
	}
	
	public String toString(){
		getIndex(0,0);
		String Board =  "  " + board.charAt(0) + "  |  " + board.charAt(1) + "  |  " + board.charAt(2) + "\n---------------\n" 
			 + "  " + board.charAt(3) + "  |  " + board.charAt(4) + "  |  " + board.charAt(5) + "\n---------------\n"
			 + "  " + board.charAt(6) + "  |  " + board.charAt(7) + "  |  " + board.charAt(8);
		if(isWinner('X') || isWinner('O')){
			Board += "\nVinneren er spiller " + player1 + "!";
			isFinished();
		}
		else if(isFinished() == true && hasWinner() == true){
			Board += "\nBrettet er fullt. Spill igjen!";
		}
		return Board;
	}
	
	public void play(int x, int y){
		if(getCurrentPlayer() == 'X'){
			setCell('X',x,y);
			this.player1 = 'X';
			undoMove.add(0, new UndoRedo(x, y, 'X'));
		}
		else if(getCurrentPlayer() == 'O'){
			setCell('O',x,y);
			this.player1 = 'O';
			undoMove.add(0, new UndoRedo(x, y, 'O'));
		}
	}
	
	public boolean isWinner(char c){
		if (c == 'X'){
			if(board.substring(0, 3) == "XXX" || board.substring(3, 6) == "XXX" || board.substring(6) == "XXX" || 
			   (board.charAt(0) == 'X' && board.charAt(4) == 'X' && board.charAt(8) == 'X') ||
			   (board.charAt(0) == 'X' && board.charAt(3) == 'X' && board.charAt(6) == 'X') ||
			   (board.charAt(1) == 'X' && board.charAt(4) == 'X' && board.charAt(7) == 'X') ||
			   (board.charAt(2) == 'X' && board.charAt(5) == 'X' && board.charAt(8) == 'X') ||
			   (board.charAt(2) == 'X' && board.charAt(4) == 'X' && board.charAt(6) == 'X')){
			   return true;
			}
		}
		else if(c == 'O'){
			if(board.substring(0, 3) == "OOO" || board.substring(3, 6) == "OOO" || board.substring(6) == "OOO" || 
			  (board.charAt(0) == 'O' && board.charAt(4) == 'O' && board.charAt(8) == 'O') ||
			  (board.charAt(0) == 'O' && board.charAt(3) == 'O' && board.charAt(6) == 'O') ||
			  (board.charAt(1) == 'O' && board.charAt(4) == 'O' && board.charAt(7) == 'O') ||
			  (board.charAt(2) == 'O' && board.charAt(5) == 'O' && board.charAt(8) == 'O') ||
			  (board.charAt(2) == 'O' && board.charAt(4) == 'O' && board.charAt(6) == 'O')){
			  return true;
			}
		}
		return false;
	}
	
	public boolean hasWinner(){
		if(isWinner('X') == true || isWinner('O') == true){
			return true;
		}
		else{
			return false;
		}
	}
	
	public boolean isFinished(){
		int blank = 0;
		for(int i = 0; i < 9; i++){
			if (board.charAt(i) == ' '){
				blank += 1;
			}
		}
		if(hasWinner() || blank == 9){
			return true;
		}
		return false;
	}
	
	public void getInput(String in){
		if (!Character.isDigit(in.charAt(0)) || !Character.isDigit(in.charAt(2))) {
			throw new IllegalArgumentException("Illegal input!");
		}
		if (in.charAt(1) != ',') {
			throw new IllegalArgumentException("Illegal input!");
		}
		play(Character.getNumericValue(in.charAt(0)),Character.getNumericValue(in.charAt(2)))	;
	}

	
	public void redo(){
		if(redoMove.size() == 0){
			throw new IllegalArgumentException("Kan ikke utføre REDO");
		}
		else{
			UndoRedo m = redoMove.get(0);
			board.setCharAt(getIndex(m.getX(), m.getY()), getCurrentPlayer());
			redoMove.remove(m);
			undoMove.add(0, m);
		}
	}
	
	public void undo(){
		if(undoMove.size() == 0){
			throw new IllegalArgumentException("Kan ikke utføre UNDO");
		}
		else{
			UndoRedo m = undoMove.get(0);
			board.setCharAt(getIndex(m.getX(), m.getY()), ' ');
			undoMove.remove(m);
			redoMove.add(0, m);
		}
	}
	
	public void save(String fileName){
		try{
			PrintWriter outFile = new PrintWriter(fileName);
			outFile.print(board);
			outFile.close();
		}
		catch (FileNotFoundException e)
        {
            System.err.println("Error: file '"+fileName+"' could not be opened for writing.");
            System.exit(1);
        }
	}
	
	public void load(String fileName){
        Scanner in;
        try
        {
            in = new Scanner(new FileReader(fileName)); 
            while(in.hasNext()){
            	String letter = in.next();
            	System.out.println(letter);
            	for (int j = 0; j < letter.length(); j++){
            		board.setCharAt(j,letter.charAt(j));          		
            	}
            } 
            in.close();
        }
        catch (FileNotFoundException e)
        {
            System.err.println("Error: file '"+fileName+"' could not be opened. Does it exist?");
            System.exit(1);
        }
    }
}