Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

L8 #464

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open

L8 #464

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
198 changes: 198 additions & 0 deletions Board.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,198 @@
package boards

import players.Player.Player

object Board {
class Board(){
var Player1Row: Array[Int] = Array(0,0,0,0,0,0)
var Player1House: Int = 0
var Player2Row: Array[Int] = Array(0,0,0,0,0,0)
var Player2House: Int = 0
val ALL_STONES = 72

def setBoard(): Unit ={
var temp : Int = ALL_STONES/12
Player1Row = Array(temp,temp,temp,temp,temp,temp)
Player1House = 0
Player2Row = Array(temp,temp,temp,temp,temp,temp)
Player2House = 0
printBoard()
}

def checkSumOfStones(): Int ={
Player1Row(0)+ Player1Row(1)+ Player1Row(2)+ Player1Row(3)+ Player1Row(4) + Player1Row(5) + Player2Row(0)+ Player2Row(1)+ Player2Row(2)+ Player2Row(3)+ Player2Row(4) + Player2Row(5) +Player2House+ Player1House
}

def someTestforPrint(): Unit ={
Player1Row(2)=12
Player2Row(3)=16
Player1House=10
}

def printBoard(): Unit ={
println(s"\n \u001B[36m${Player1Row(0)} ${Player1Row(1)} ${Player1Row(2)} ${Player1Row(3)} ${Player1Row(4)} ${Player1Row(5)}"+
s"\n\u001B[1m${Player1House} \u001B[32m${Player2House}\u001B[0m "+
s"\n \u001B[32m${Player2Row(0)} ${Player2Row(1)} ${Player2Row(2)} ${Player2Row(3)} ${Player2Row(4)} ${Player2Row(5)}\u001B[0m");

}

def moveInFirstRow(stones:Int, player:Int, start:Int):Int={
if player==1 then{
if (start == -1 ) stones
else{
var i =start
var newSt = stones
while(newSt!=0 && i>=0){
if Player1Row(i) == 0 then {
Player1House = Player1House + Player2Row(i)
Player2Row(i) = 0;
}
Player1Row(i) += 1
i -= 1
newSt -= 1
}
newSt
}
}
else{
var i =start
var newSt = stones
while(newSt!=0 && i>=0){
Player1Row(i) += 1
i -= 1
newSt -= 1
}
newSt
}
}

def moveInSecRow(stones: Int, player: Int, start:Int):Int={
if player==1 then{
var i =start
var newSt = stones
while(newSt!=0 && i<Player2Row.size){
Player2Row(i) += 1
i += 1
newSt -= 1
}
newSt
}
else{
if (start==Player2Row.size) stones
else{
var i =start
var newSt = stones
while(newSt!=0 && i<Player2Row.size){
if Player2Row(i)==0 then{
Player2House = Player2House + Player1Row(i)
Player1Row(i) = 0;
}
Player2Row(i) += 1
i+=1
newSt -= 1
}
newSt
}
}
}


def movingPieces(index:Int, row:Int): Boolean ={
if(row == 1) then {
var stones = Player1Row(index)
Player1Row(index) = 0
var i = index - 1
//if i == -1 then i = 0
while (stones != 0) {
stones = moveInFirstRow(stones, 1, i)
if (stones == 1) {
Player1House += 1
return true
}

else if (stones > 1) {
Player1House += 1
stones -= 1
i = 0
}
stones = moveInSecRow(stones, 1, i)
}
false
}
else {
var stones = Player2Row(index)
Player2Row(index) = 0
var i = index + 1
while(stones!=0){
stones = moveInSecRow(stones, 2, i)

if (stones == 1) {
Player2House += 1
return true
}

else if(stones>1){
Player2House +=1
stones-=1
i = Player1Row.size-1
}

stones = moveInFirstRow(stones, 2, i)

}

false

}
}

def gameOver(): Unit ={
var winner =0
if Player1House>Player2House then winner =1
else if Player2House>Player1House then winner =2
else println(s"Draw!")
if winner!=0 then println(s"Player$winner won!")
}

def checkRow(row: Array[Int]):Boolean={
var i =0
var j=0
while(i<row.size){
if row(i)==0 then j+=1
i+=1
}
if j==6 then true
else false
}

def endOfTheGame(): Boolean ={
if(Player1House>ALL_STONES/2 || Player2House>ALL_STONES/2) then true
else if(checkRow(Player1Row)||checkRow(Player2Row)) then true
else false
}

def copyRow(row: Array[Int]): Array[Int]={
var temp: Array[Int] = new Array[Int](row.size)
var index = 0
while(index>row.size){
temp(index) = row(index)
}
temp
}

def copy(): Board ={
var temp = new Board()
temp.Player1House = Player1House
temp.Player2House = Player2House




temp.Player1Row = Player1Row.clone()
temp.Player2Row = Player2Row.clone()
temp

}
}

}
57 changes: 57 additions & 0 deletions Game.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package game

import boards.Board.Board
import players.Player.{AI, HumanPlayer, Player, SimulationPlayer}

object Game {
def playing(board: Board, player1: Player, player2: Player): Unit ={
board.setBoard()
var round =0;
while(!board.endOfTheGame()){
var x = false
println(s"ROUND $round")
//println("stones: "+board.checkSumOfStones())
if(round%2==0){
var check = player1.move()
while(board.Player1Row(check)==0){
check = player1.move()
}
println("Move Player1: "+check)
x =board.movingPieces(check, 1)
}
else{
var check = player2.move()
while(board.Player2Row(check)==0){
check = player2.move()
}
println("Move Player2: "+check)
x =board.movingPieces(check, 2)
}
if x==false then round+=1
board.printBoard()
}
board.gameOver()
}

def Simulation(): Unit ={
var board = new Board()
var player1 = new SimulationPlayer(board, 1)
var player2 = new SimulationPlayer(board, 2)
playing(board, player1, player2)
}

def SimualtionVSHuman(): Unit ={
var board = new Board()
var player1 = new HumanPlayer(board, 1)
var player2 = new SimulationPlayer(board, 2)
playing(board, player1, player2)
}

def AIVSHuman(): Unit ={
var board = new Board()
var player1 = new HumanPlayer(board, 1)
var player2 = new AI(board, 2)
playing(board, player1, player2)
}

}
Loading