diff --git a/.DS_Store b/.DS_Store new file mode 100644 index 0000000..9369387 Binary files /dev/null and b/.DS_Store differ diff --git a/Sources/.DS_Store b/Sources/.DS_Store new file mode 100644 index 0000000..5008ddf Binary files /dev/null and b/Sources/.DS_Store differ diff --git a/Sources/KonaneColor.swift b/Sources/KonaneColor.swift index 9e9cfcb..8e274d7 100644 --- a/Sources/KonaneColor.swift +++ b/Sources/KonaneColor.swift @@ -1,3 +1,13 @@ +// +// KonaneColor.swift +// +// +// Created by Alex Hill on 10/20/16. +// +// + +import Foundation + enum KonaneColor { - case black, white, empty -} + case black, white, empty +} \ No newline at end of file diff --git a/Sources/KonaneGame.swift b/Sources/KonaneGame.swift index ac4d205..f0abde2 100644 --- a/Sources/KonaneGame.swift +++ b/Sources/KonaneGame.swift @@ -1,96 +1,157 @@ +// +// KonaneGame.swift +// +// +// Created by Alex Hill on 10/20/16. +// +// + +import Foundation + +/* +KonaneGame +x - init(blackIsHuman: Bool, whiteIsHuman: Bool) +x - private gameState: KonaneGameState +x - private blackInputSource: KonaneMoveInputSource +x - private whiteInputSource: KonaneMoveInputSource + - play() -> Bool // Returns true if black wins. + - private displayBoard() +*/ + + + + + +//WILL FIX LATER FIRST GETTING INITIALIZATION DONE class KonaneGame { - private let gameState: KonaneGameState = KonaneGameState() + + private var gameState = KonaneGameState(width: 6, height: 6, isBlackTurn: true) //May have broken it private let blackInputSource: KonaneMoveInputSource private let whiteInputSource: KonaneMoveInputSource + init(blackIsHuman: Bool, whiteIsHuman: Bool) { + if blackIsHuman { blackInputSource = KonaneMoveInputSourceHuman(isBlack: true) - } else { - fatalError("Doesn't work yet!") } - + else { + //FIX ONCE AI CODE EXISTS + blackInputSource = KonaneMoveInputSourceAI(isBlack: true) + } if whiteIsHuman { whiteInputSource = KonaneMoveInputSourceHuman(isBlack: false) - } else { - fatalError("Doesn't work yet!") + } + else { + //FIX ONCE AI CODE EXISTS + whiteInputSource = KonaneMoveInputSourceAI(isBlack: false) } } - func play() -> Bool { - // Remove Phase - print("Black starts by removing a middle or corner black piece") - displayBoard() - - let blackRemovePoint = blackInputSource.removeFirstPiece(gameState: gameState.copy()) - gameState.perform(blackRemove: blackRemovePoint) - - print("Now white removes an adjacent white piece") - displayBoard() - - let whiteRemovePoint = whiteInputSource.removeSecondPiece(gameState: gameState.copy()) - gameState.perform(whiteRemove: whiteRemovePoint) + func displayBoard() { + var arbitraryVariable = 0 - // Now, main game loop - while gameState.didBlackWin() == false && gameState.didWhiteWin() == false { - - let move: KonaneMove - if gameState.getIsBlackTurn() { - print("It is black's turn to jump") - displayBoard() - move = blackInputSource.nextMove(gameState: gameState.copy()) - } else { - print("It is white's turn to jump") - displayBoard() - move = whiteInputSource.nextMove(gameState: gameState.copy()) + for rowNumber in (0.. (xCoord: Int, yCoord: Int) { + while true { + let x = Int(readLine()!) + let y = Int(readLine()!) + if x != nil && y != nil && x! < gameState.height && y! < gameState.height && x! >= 0 && y! >= 0 { + return (xCoord: x!, yCoord: y!) + } else { + print("This is not a valid input. Try again") + } + } } + + + func play() -> Bool { - private func displayASCIIBoard() { - for y in (0.. Bool +x - color(atX: Int, atY: Int) -> KonaneColor // Bottom left, 0-indexed +x - isValid(move: KonaneMove) -> Bool +x - isValid(blackRemove: (x: Int, y: Int)) -> Bool +x - isValid(whiteRemove: (x: Int, y: Int)) -> Bool +x - perform(move: KonaneMove) +x - perform(blackRemove: (x: Int, y: Int)) +x - perform(whiteRemove: (x: Int, y: Int)) +x - didBlackWin() -> Bool +x - didWhiteWin() -> Bool +*/ + +//WILL need to remove enum before build +//SINCE it is included in KonaneColor.swift class KonaneGameState { - let width = 16 - let height = 16 - private var board: [[KonaneColor]] // Each inner array is a column on the board - private var isBlackTurn = true + var width: Int + var height: Int + var isBlackTurn: Bool - init() { - let singleCol = [KonaneColor](repeating: KonaneColor.empty, count: height) + init (width: Int, height: Int, isBlackTurn: Bool) { + self.width = width + self.height = height + self.isBlackTurn = isBlackTurn + } - board = [[KonaneColor]](repeating: singleCol, count: width) + public var gameBoard: [[KonaneColor]] = [] - // Checkerboard - for x in 0.. black first + if row % 2 == 0 { + gameBoard[column].append(KonaneColor.black) + } + else { + gameBoard[column].append(KonaneColor.white) + } + } + else { + if row % 2 == 0 { //If column odd -> white first + gameBoard[column].append(KonaneColor.white) + } + else { + gameBoard[column].append(KonaneColor.black) + } } } } } - func copy() -> KonaneGameState { - let c = KonaneGameState() - c.board = board - c.isBlackTurn = isBlackTurn - return c - } - func getIsBlackTurn() -> Bool { - return isBlackTurn + if isBlackTurn == true { + return true + } + else { + return false + } } func color(atX: Int, atY: Int) -> KonaneColor { - return board[atX][atY] + return gameBoard[atX][atY] } + // + //ADD ALL ELSE STATEMENTS func isValid(move: KonaneMove) -> Bool { - print("Doesn't work yet") - return true + /* + Criteria for this function + */ + + /* Criteria: + - Is the place to jump to within the gameBoard? + + move returns 4 variables: + move.fromX + move.fromY + move.toX + move.toY + + // a move is valid if + x - the the move.toX and move.toY is empty + x - there is a piece between move.toX and move.toY and move.fromX and move.fromY + x - include double jump + x - is there a piece between each jump + x - if it is a valid gameBoard place + x - the space to move to is empty + */ + var returnBool = false + if isBlackTurn == true { + //How do I correctly access the gameBoard and the enum? - FIX + //Check if this is a valid piece for the user to move + if gameBoard[move.fromX][move.fromY] == KonaneColor.black { + //Is this how I actually add values? + //Checks if there is a place to move to + //Checks for verticle direction + if move.fromX == move.toX { + if move.toY > move.fromY { + if gameBoard[move.fromX][move.fromY + 1] == KonaneColor.white && gameBoard[move.toX][move.toY] == KonaneColor.empty { + if abs(move.toX - move.fromX) == 4 || abs(move.toY - move.fromY) == 4 { + if gameBoard[move.toX][move.toY - 1] == KonaneColor.white { + returnBool = true + } + } else { + returnBool = true + } + } + } else if move.toY < move.fromY { + if gameBoard[move.fromX][move.fromY - 1] == KonaneColor.white && gameBoard[move.toX][move.toY] == KonaneColor.empty { + if abs(move.toX - move.fromX) == 4 || abs(move.toY - move.fromY) == 4 { + if gameBoard[move.toX][move.toY + 1] == KonaneColor.white { + returnBool = true + } + } else { + returnBool = true + } + } + } + } else if move.fromY == move.toY { + if move.toX > move.fromX { + if gameBoard[move.fromX + 1][move.fromY] == KonaneColor.white && gameBoard[move.toX][move.toY] == KonaneColor.empty { + if abs(move.toX - move.fromX) == 4 || abs(move.toY - move.fromY) == 4 { + if gameBoard[move.toX - 1][move.toY] == KonaneColor.white { + returnBool = true + } + } else { + returnBool = true + } + } + } else if move.toX < move.fromX { + if gameBoard[move.fromX - 1][move.fromY] == KonaneColor.white && gameBoard[move.toX][move.toY] == KonaneColor.empty { + if abs(move.toX - move.fromX) == 4 || abs(move.toY - move.fromY) == 4 { + if gameBoard[move.toX + 1][move.toY] == KonaneColor.white { + returnBool = true + } + } else { + returnBool = true + } + } + } + } + } + } else if isBlackTurn == false{ + //Check if this is a valid piece for the user to move + if gameBoard[move.fromX][move.fromY] == KonaneColor.white { + //Is this how I actually add values? + //Checks if there is a place to move to + //Checks for verticle direction + if move.fromX == move.toX { + if move.toY > move.fromY { + if gameBoard[move.fromX][move.fromY + 1] == KonaneColor.black && gameBoard[move.toX][move.toY] == KonaneColor.empty { + if abs(move.toX - move.fromX) == 4 || abs(move.toY - move.fromY) == 4 { + if gameBoard[move.toX][move.toY - 1] == KonaneColor.black { + returnBool = true + } + } else { + returnBool = true + } + } + } else if move.toY < move.fromY { + if gameBoard[move.fromX][move.fromY - 1] == KonaneColor.black && gameBoard[move.toX][move.toY] == KonaneColor.empty { + if abs(move.toX - move.fromX) == 4 || abs(move.toY - move.fromY) == 4 { + if gameBoard[move.toX][move.toY + 1] == KonaneColor.black { + returnBool = true + } + } else { + returnBool = true + } + } + } + } else if move.fromY == move.toY { + if move.toX > move.fromX { + if gameBoard[move.fromX + 1][move.fromY] == KonaneColor.black && gameBoard[move.toX][move.toY] == KonaneColor.empty { + if abs(move.toX - move.fromX) == 4 || abs(move.toY - move.fromY) == 4 { + if gameBoard[move.toX - 1][move.toY] == KonaneColor.black { + returnBool = true + } + } else { + returnBool = true + } + } + } else if move.toX < move.fromX { + if gameBoard[move.fromX - 1][move.fromY] == KonaneColor.black && gameBoard[move.toX][move.toY] == KonaneColor.empty { + if abs(move.toX - move.fromX) == 4 || abs(move.toY - move.fromY) == 4 { + if gameBoard[move.toX + 1][move.toY] == KonaneColor.black { + returnBool = true + } + } else { + returnBool = true + } + } + } + } + } + } + return returnBool } - - func isValid(blackRemove: (x: Int, y: Int)) -> Bool { - print("Doesn't work yet") - return true + //IDK what this does + //Done? + func isValid(blackRemove: (x: Int, y: Int)) -> Bool { + var returnBool = false + if isBlackTurn == true{ + for column in 0.. Bool { - print("Doesn't work yet") - return true + //Done? + //Checking the index value of an array that does not exist + func isValid(whiteRemove: (x: Int, y: Int)) -> Bool { + if gameBoard[whiteRemove.x][whiteRemove.y] == KonaneColor.white { + //If the piece chosen is not in the left bottom corner, only test up and right + if whiteRemove.x < 1 { + if gameBoard[whiteRemove.x + 1][whiteRemove.y] == KonaneColor.empty || gameBoard[whiteRemove.x][whiteRemove.y + 1] == KonaneColor.empty || gameBoard[whiteRemove.x][whiteRemove.y - 1] == KonaneColor.empty { + return true + } + } else if whiteRemove.x > width - 2 { + if gameBoard[whiteRemove.x - 1][whiteRemove.y] == KonaneColor.empty || gameBoard[whiteRemove.x][whiteRemove.y + 1] == KonaneColor.empty || gameBoard[whiteRemove.x][whiteRemove.y - 1] == KonaneColor.empty { + return true + } + } else if whiteRemove.y < 1 { + if gameBoard[whiteRemove.x][whiteRemove.y + 1] == KonaneColor.empty || gameBoard[whiteRemove.x + 1][whiteRemove.y] == KonaneColor.empty || gameBoard[whiteRemove.x - 1][whiteRemove.y] == KonaneColor.empty { + return true + } + } else if whiteRemove.y > height - 2 { + if gameBoard[whiteRemove.x][whiteRemove.y - 1] == KonaneColor.empty || gameBoard[whiteRemove.x + 1][whiteRemove.y] == KonaneColor.empty || gameBoard[whiteRemove.x - 1][whiteRemove.y] == KonaneColor.empty { + return true + } + } else if whiteRemove.x > 0 && whiteRemove.x < width - 1 && whiteRemove.y > 0 && whiteRemove.y < height - 1 { + if gameBoard[whiteRemove.x][whiteRemove.y + 1] == KonaneColor.empty || gameBoard[whiteRemove.x][whiteRemove.y - 1] == KonaneColor.empty || gameBoard[whiteRemove.x + 1][whiteRemove.y] == KonaneColor.empty || gameBoard[whiteRemove.x - 1][whiteRemove.y] == KonaneColor.empty { + return true + } + } else { + print("Not a valid piece to remove") + return false + } + } else { + print("error with checking spaces around tile") + return false + } + print("That is not a white piece") + return false } func perform(move: KonaneMove) { - print("Doesn't work yet!") - isBlackTurn = !isBlackTurn + if isValid(move: move) { + gameBoard[move.fromX][move.fromY] = KonaneColor.empty + if isBlackTurn == true{ + gameBoard[move.toX][move.toY] = KonaneColor.black + if move.fromX == move.toX { + if move.toY > move.fromY { + gameBoard[move.toX][move.toY - 1] = KonaneColor.empty + if abs(move.toY - move.fromY) == 4 { + gameBoard[move.toX][move.fromY + 1] = KonaneColor.empty + } + } else { + gameBoard[move.toX][move.toY + 1] = KonaneColor.empty + if abs(move.toY - move.fromY) == 4 { + gameBoard[move.toX][move.fromY - 1] = KonaneColor.empty + } + } + } else { + if move.toX > move.fromX { + gameBoard[move.toX - 1][move.toY] = KonaneColor.empty + if abs(move.toX - move.fromX) == 4 { + gameBoard[move.fromX + 1][move.fromY] = KonaneColor.empty + } + } else { + gameBoard[move.toX + 1][move.toY] = KonaneColor.empty + if abs(move.toX - move.fromX) == 4 { + gameBoard[move.fromX - 1][move.fromY] = KonaneColor.empty + } + } + } + } else if isBlackTurn == false{ + gameBoard[move.toX][move.toY] = KonaneColor.white + if move.fromX == move.toX { + if move.toY > move.fromY { + gameBoard[move.toX][move.toY - 1] = KonaneColor.empty + if abs(move.toY - move.fromY) == 4 { + gameBoard[move.toX][move.fromY + 1] = KonaneColor.empty + } + } else { + gameBoard[move.toX][move.toY + 1] = KonaneColor.empty + if abs(move.toY - move.fromY) == 4 { + gameBoard[move.toX][move.fromY - 1] = KonaneColor.empty + } + } + } else { + if move.toX > move.fromX { + gameBoard[move.toX - 1][move.toY] = KonaneColor.empty + if abs(move.toX - move.fromX) == 4 { + gameBoard[move.fromX + 1][move.fromY] = KonaneColor.empty + } + } else { + gameBoard[move.toX + 1][move.toY] = KonaneColor.empty + if abs(move.toX - move.fromX) == 4 { + gameBoard[move.fromX - 1][move.fromY] = KonaneColor.empty + } + } + } + } else { + print("Error in perform(move:) function") + } + } + } + + func perform(blackRemove: (xCoord: Int, yCoord: Int)) { + if isValid(blackRemove: (x: blackRemove.xCoord, y: blackRemove.yCoord)) { + gameBoard[blackRemove.xCoord][blackRemove.yCoord] = KonaneColor.empty + } else { + print("error") + } } - func perform(blackRemove: (x: Int, y: Int)) { - var remove = blackRemove - if !isValid(blackRemove: remove) { - fatalError("Invalid black remove: \(remove)") + func perform(whiteRemove: (xCoord: Int, yCoord: Int)) { + if isValid(whiteRemove: (x: whiteRemove.xCoord, y: whiteRemove.yCoord)) { + gameBoard[whiteRemove.xCoord][whiteRemove.yCoord] = KonaneColor.empty + } else { + print("error") } - - board[remove.x][remove.y] = KonaneColor.empty } + + - func perform(whiteRemove: (x: Int, y: Int)) { - var remove = whiteRemove - if !isValid(whiteRemove: remove) { - fatalError("Invalid white remove: \(remove)") + func didBlackWin() -> Bool { + if getIsBlackTurn() == true { + for column in 0.. height - 3{ + if (gameBoard[column + 1][row] == KonaneColor.white && gameBoard[column + 2][row] == KonaneColor.empty) || + (gameBoard[column][row - 1] == KonaneColor.white && gameBoard[column][row - 2] == KonaneColor.empty) { + return false + } + //tests top right square of 4 + } else if column > width - 3 && row > height - 3{ + if (gameBoard[column - 1][row] == KonaneColor.white && gameBoard[column - 2][row] == KonaneColor.empty) || + (gameBoard[column][row - 1] == KonaneColor.white && gameBoard[column][row - 2] == KonaneColor.empty) { + return false + } + //tests bottom left + } else if row < 2 && column < 2{ + if (gameBoard[column][row + 1] == KonaneColor.white && gameBoard[column][row + 2] == KonaneColor.empty) || + (gameBoard[column + 1][row] == KonaneColor.white && gameBoard[column + 2][row] == KonaneColor.empty) { + return false + } + //tests bottom right + } else if row > height - 3 && column > width - 3{ + if (gameBoard[column][row + 1] == KonaneColor.white && gameBoard[column][row + 2] == KonaneColor.empty) || + (gameBoard[column - 1][row] == KonaneColor.white && gameBoard[column - 2][row] == KonaneColor.empty) { + return false + } + //tests not corners and not center board. Does test left side + } else if 1 < row && row < height - 3 && column < 2 { + if (gameBoard[column][row + 1] == KonaneColor.white && gameBoard[column][row + 2] == KonaneColor.empty) || + (gameBoard[column + 1][row] == KonaneColor.white && gameBoard[column + 2][row] == KonaneColor.empty) || + (gameBoard[column][row - 1] == KonaneColor.white && gameBoard[column][row - 2] == KonaneColor.empty) { + return false + } + //tests not corners and not center. Does test right side + } else if 1 < row && row < height - 3 && column > width - 3 { + if (gameBoard[column][row + 1] == KonaneColor.white && gameBoard[column][row + 2] == KonaneColor.empty) || + (gameBoard[column - 1][row] == KonaneColor.white && gameBoard[column - 2][row] == KonaneColor.empty) || + (gameBoard[column][row - 1] == KonaneColor.white && gameBoard[column][row - 2] == KonaneColor.empty) { + return false + } + //tests not corners and not center. Does test up + } else if row > height - 3 && 1 < column && column < width - 3 { + if (gameBoard[column + 1][row] == KonaneColor.white && gameBoard[column + 2][row] == KonaneColor.empty) || + (gameBoard[column - 1][row] == KonaneColor.white && gameBoard[column - 2][row] == KonaneColor.empty) || + (gameBoard[column][row - 1] == KonaneColor.white && gameBoard[column][row - 2] == KonaneColor.empty) { + return false + } + // tests not corners and not conter. Does test down + } else if row < 2 && 1 < column && column < width - 3 { + if (gameBoard[column + 1][row] == KonaneColor.white && gameBoard[column + 2][row] == KonaneColor.empty) || + (gameBoard[column - 1][row] == KonaneColor.white && gameBoard[column - 2][row] == KonaneColor.empty) || + (gameBoard[column][row + 1] == KonaneColor.white && gameBoard[column][row + 2] == KonaneColor.empty) { + return false + } + } + } + } + } + } else { + return false } - - board[remove.x][remove.y] = KonaneColor.empty + return true } - func didBlackWin() -> Bool { - print("Doesn't work yet!") - return false + func didWhiteWin() -> Bool { + if getIsBlackTurn() == false { + for column in 0.. height - 3{ + if (gameBoard[column + 1][row] == KonaneColor.black && gameBoard[column + 2][row] == KonaneColor.empty) || + (gameBoard[column][row - 1] == KonaneColor.black && gameBoard[column][row - 2] == KonaneColor.empty) { + return false + } + //tests top right square of 4 + } else if column > width - 3 && row > height - 3{ + if (gameBoard[column - 1][row] == KonaneColor.black && gameBoard[column - 2][row] == KonaneColor.empty) || + (gameBoard[column][row - 1] == KonaneColor.black && gameBoard[column][row - 2] == KonaneColor.empty) { + return false + } + //tests bottom left + } else if row < 2 && column < 2{ + if (gameBoard[column][row + 1] == KonaneColor.black && gameBoard[column][row + 2] == KonaneColor.empty) || + (gameBoard[column + 1][row] == KonaneColor.black && gameBoard[column + 2][row] == KonaneColor.empty) { + return false + } + //tests bottom right + } else if row > height - 3 && column > width - 3{ + if (gameBoard[column][row + 1] == KonaneColor.black && gameBoard[column][row + 2] == KonaneColor.empty) || + (gameBoard[column - 1][row] == KonaneColor.black && gameBoard[column - 2][row] == KonaneColor.empty) { + return false + } + //tests not corners and not center board. Does test left side + } else if 1 < row && row < height - 3 && column < 2 { + if (gameBoard[column][row + 1] == KonaneColor.black && gameBoard[column][row + 2] == KonaneColor.empty) || + (gameBoard[column + 1][row] == KonaneColor.black && gameBoard[column + 2][row] == KonaneColor.empty) || + (gameBoard[column][row - 1] == KonaneColor.black && gameBoard[column][row - 2] == KonaneColor.empty) { + return false + } + //tests not corners and not center. Does test right side + } else if 1 < row && row < height - 3 && column > width - 3 { + if (gameBoard[column][row + 1] == KonaneColor.black && gameBoard[column][row + 2] == KonaneColor.empty) || + (gameBoard[column - 1][row] == KonaneColor.black && gameBoard[column - 2][row] == KonaneColor.empty) || + (gameBoard[column][row - 1] == KonaneColor.black && gameBoard[column][row - 2] == KonaneColor.empty) { + return false + } + //tests not corners and not center. Does test up + } else if row > height - 3 && 1 < column && column < width - 3 { + if (gameBoard[column + 1][row] == KonaneColor.black && gameBoard[column + 2][row] == KonaneColor.empty) || + (gameBoard[column - 1][row] == KonaneColor.black && gameBoard[column - 2][row] == KonaneColor.empty) || + (gameBoard[column][row - 1] == KonaneColor.black && gameBoard[column][row - 2] == KonaneColor.empty) { + return false + } + // tests not corners and not conter. Does test down + } else if row < 2 && 1 < column && column < width - 3 { + if (gameBoard[column + 1][row] == KonaneColor.black && gameBoard[column + 2][row] == KonaneColor.empty) || + (gameBoard[column - 1][row] == KonaneColor.black && gameBoard[column - 2][row] == KonaneColor.empty) || + (gameBoard[column][row + 1] == KonaneColor.black && gameBoard[column][row + 2] == KonaneColor.empty) { + return false + } + } + } + } + } + } else { + return false + } + return true } - func didWhiteWin() -> Bool { - print("Doesn't work yet!") - return false + func copy() -> KonaneGameState { + let c = KonaneGameState() + c. = self. + ... + ... + + return c } -} +} \ No newline at end of file diff --git a/Sources/KonaneMove.swift b/Sources/KonaneMove.swift index 610e0f9..6fc3b35 100644 --- a/Sources/KonaneMove.swift +++ b/Sources/KonaneMove.swift @@ -1,14 +1,33 @@ +// +// KonaneMove.swift +// +// +// Created by Alex Hill on 10/20/16. +// +// + +import Foundation + +/* +KonaneMove +x - init(fromX: Int, fromY: Int, toX: Int, toY: Int) +x - fromX +x - fromY +x - toX +x - toY +*/ + class KonaneMove { - let fromX: Int - let fromY: Int - let toX: Int - let toY: Int - - init(fromX: Int, fromY: Int, toX: Int, toY: Int) { - self.fromX = fromX - self.fromY = fromY - self.toX = toX - self.toY = toY - } + let fromX: Int + let fromY: Int + let toX: Int + let toY: Int + + init (fromX: Int, fromY: Int, toX: Int, toY: Int) { + self.fromX = fromX + self.fromY = fromY + self.toX = toX + self.toY = toY + } } diff --git a/Sources/KonaneMoveInputSource.swift b/Sources/KonaneMoveInputSource.swift index a2554b4..2b6d0f6 100644 --- a/Sources/KonaneMoveInputSource.swift +++ b/Sources/KonaneMoveInputSource.swift @@ -1,19 +1,43 @@ -class KonaneMoveInputSource { - let isBlack: Bool +// +// KonaneMoveInputSource.swift +// +// +// Created by Alex Hill on 10/20/16. +// +// + +import Foundation + +/* +KonaneMoveInputSource +- init(isBlack: Bool) +- isBlack +- removeFirstPiece(gameState: KonaneGameState) -> (x: Int, y: Int) (should override) +- removeSecondPiece(gameState: KonaneGameState) -> (x: Int, y: Int) (should override) +- nextMove(gameState: KonaneGameState) -> KonaneMove - init(isBlack: Bool) { - self.isBlack = isBlack - } +Subclasses: +KonaneMoveInputSourceHuman +[TeamName]_KonaneMoveInputSourceAI +*/ + +class KonaneMoveInputSource { + var isBlack: Bool - func removeFirstPiece(gameState: KonaneGameState) -> (x: Int, y: Int) { - fatalError("Override me!") - } + init(isBlack: Bool) { + self.isBlack = isBlack + } + // Placeholder functions defined in subclasses + func removeFirstPiece(gameState: KonaneGameState) -> (xCoord: Int, yCoord: Int) { + return(xCoord: 1, yCoord: 1) + } - func removeSecondPiece(gameState: KonaneGameState) -> (x: Int, y: Int) { - fatalError("Override me!") - } + func removeSecondPiece(gameState: KonaneGameState) -> (xCoord: Int, yCoord: Int) { + return(xCoord: 1, yCoord: 1) + } - func nextMove(gameState: KonaneGameState) -> KonaneMove { - fatalError("Override me!") - } + func nextMove(gameState: KonaneGameState) -> KonaneMove { + let move = KonaneMove(fromX: 1, fromY: 1, toX: 1, toY: 1) + return(move) + } } diff --git a/Sources/KonaneMoveInputSourceAI.swift b/Sources/KonaneMoveInputSourceAI.swift new file mode 100644 index 0000000..431bb41 --- /dev/null +++ b/Sources/KonaneMoveInputSourceAI.swift @@ -0,0 +1,113 @@ +// +// KonaneMoveInputSourceAIcomputer.swift +// +// +// Created by Alex Hill on 10/20/16. +// +// + + +// Use minimax somehow lmao +import Foundation + +class KonaneMoveInputSourceAI: KonaneMoveInputSource { + + override func removeFirstPiece(gameState: KonaneGameState) -> (xCoord: Int, yCoord: Int) { + return (xCoord: 0, yCoord: 0) + } + + override func removeSecondPiece(gameState: KonaneGameState) -> (xCoord: Int, yCoord: Int) { + return (xCoord: 1, yCoord: 0) + } + + override func nextMove(gameState: KonaneGameState) -> KonaneMove { + let currentGame = gameState.copy() + + var advancedGame = gameState.copy() + + var possibleMoves: [KonaneMove] + var responses: Int + var bestResponse: Int = 1000000000 //Needs to be big + var bestMove: KonaneMove + + if currentGame.getIsBlackTurn() { + + for fromX in 0.. (x: Int, y: Int) { +// +// KonaneMoveInputSourceHuman.swift +// +// +// Created by Alex Hill on 10/20/16. +// +// + +import Foundation + +class KonaneMoveInputSourceHuman: KonaneMoveInputSource { + + func askForInput(gameState: KonaneGameState) -> (xCoord: Int, yCoord: Int) { while true { - print("Input x coord to remove black piece:") - let x = Int(readLine()!)! - print("Input y coord to remove black piece:") - let y = Int(readLine()!)! - - if gameState.isValid(blackRemove: (x, y)) { - return (x, y) + let x = Int(readLine()!) + let y = Int(readLine()!) + if x != nil && y != nil && x! < gameState.height && y! < gameState.height && x! >= 0 && y! >= 0 { + return (xCoord: x!, yCoord: y!) + } else { + print("This is not a valid input. Try again") } } } + - override func removeSecondPiece(gameState: KonaneGameState) -> (x: Int, y: Int) { + override func removeFirstPiece(gameState: KonaneGameState) -> (xCoord: Int, yCoord: Int) { + var removeBlackTuple: (xCoord: Int, yCoord: Int) + while true { + print("What are the coordinates of the piece player1 (x's) wants to remove?") + removeBlackTuple = askForInput(gameState: gameState) + + if gameState.isValid(blackRemove: (x: removeBlackTuple.xCoord, y: removeBlackTuple.yCoord)) { + return removeBlackTuple + } else { + print("Not a valid move") + } + } + } + + + override func removeSecondPiece(gameState: KonaneGameState) -> (xCoord: Int, yCoord: Int) { + var removeWhiteTuple: (xCoord: Int, yCoord: Int) while true { - print("Input x coord to remove white piece:") - let x = Int(readLine()!)! - print("Input y coord to remove white piece:") - let y = Int(readLine()!)! + print("What are the coordinates of the piece player2 (o's) wants to remove?") + removeWhiteTuple = askForInput(gameState: gameState) - if gameState.isValid(whiteRemove: (x, y)) { - return (x, y) + if gameState.isValid(whiteRemove: (x: removeWhiteTuple.xCoord, y: removeWhiteTuple.yCoord)) { + return removeWhiteTuple + } else { + print("Not a valid move") } } } - + + override func nextMove(gameState: KonaneGameState) -> KonaneMove { - print("Input x coord to move from:") - let fx = Int(readLine()!)! - print("Input y coord to move from:") - let fy = Int(readLine()!)! - - print("Input x coord to move to:") - let tx = Int(readLine()!)! - print("Input y coord to move to:") - let ty = Int(readLine()!)! + + var move: KonaneMove = KonaneMove(fromX: 0, fromY: 0, toX: 0, toY: 0) - - return KonaneMove(fromX: fx, fromY: fy, toX: tx, toY: ty) + if gameState.isBlackTurn { + + while true { + print("What are the coordinates of the piece player1 (x's) wants to move?") + let fromCoordTuple = askForInput(gameState: gameState) + print("Where do you want to move that tile too?") + let toCoordTuple = askForInput(gameState: gameState) + + move = KonaneMove(fromX: fromCoordTuple.xCoord, fromY: fromCoordTuple.yCoord, toX: toCoordTuple.xCoord, toY: toCoordTuple.yCoord) + + if gameState.isValid(move: move) { + return move + } else { + print("That was not a valid move, try again") + } + } + } else if !gameState.isBlackTurn { + while true { + print("What are the coordinates of the piece player2 (o's) wants to move?") + let fromCoordTuple = askForInput(gameState: gameState) + print("Where do you want to move that tile too?") + let toCoordTuple = askForInput(gameState: gameState) + + move = KonaneMove(fromX: fromCoordTuple.xCoord, fromY: fromCoordTuple.yCoord, toX: toCoordTuple.xCoord, toY: toCoordTuple.yCoord) + if gameState.isValid(move: move) { + return move + } else { + print("Error in play function") + } + } + } + print("ERROR ERROR ERROR: nextMove defualted to (fromX: 0, fromY: 0, toX: 0, toY: 0)") + return move } + + + + } diff --git a/Sources/main.swift b/Sources/main.swift index eddd5e8..ecedd09 100644 --- a/Sources/main.swift +++ b/Sources/main.swift @@ -1,2 +1,2 @@ let game = KonaneGame(blackIsHuman: true, whiteIsHuman: true) -print("Did black win? \(game.play())") +game.play()