Created
September 24, 2018 12:56
-
-
Save srisankethu/4afbb2523c985a6d71a955ff59c26064 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"math/rand" | |
"fmt" | |
) | |
type Cell struct { | |
X int | |
Y int | |
Power int | |
StomachFilling int | |
Team int | |
} | |
func (e Cell) move(newX int, newY int) { | |
// increase or decrease Cell coordinates by 1 | |
// INSERT YOUR CODE HERE ####### | |
var index int | |
index = find(e.X, e.Y) | |
e.X = newX | |
e.Y = newY | |
ArCells[index] = e | |
} | |
func (e Cell) devour(victim Cell) { | |
// Consumes a closely standing victim if it's not more powerful then the current cell. | |
// Stomach filling increases by the power of the victim but can't be more than current Cell power. | |
// Victims must belong to a different team | |
// INSERT YOUR CODE HERE ####### | |
if e.StomachFilling + victim.Power >= e.Power { | |
e.StomachFilling = e.Power | |
}else { | |
e.StomachFilling += victim.Power | |
} | |
// Remove the victim Cell from ArCells | |
victim.remove() | |
} | |
func (e Cell) divide(direction string) { | |
// add a new Cell with power not more than current stomach filling | |
// a current Cell power increases by a new Cell power | |
// a current StomachFilling decreases by a new Cell power | |
// a new cell will appear on the right, left, top or bottom side of the current Cell | |
// (it depends on a direction value) | |
// ArCells = append(ArCells, newChildCell) | |
// INSERT YOUR CODE HERE ####### | |
var newChildCell Cell | |
newChildCell.Power = rand.Intn(e.StomachFilling) | |
newChildCell.StomachFilling = newChildCell.Power | |
e.Power += newChildCell.Power | |
e.StomachFilling -= newChildCell.Power | |
if direction == "top"{ | |
newChildCell.move(e.X, e.Y + 1) | |
}else if direction == "bottom" { | |
newChildCell.move(e.X, e.Y - 1) | |
}else if direction == "left" { | |
newChildCell.move(e.X - 1, e.Y) | |
}else if direction == "right" { | |
newChildCell.move(e.X + 1, e.Y) | |
} | |
ArCells = append(ArCells, newChildCell) | |
} | |
func (e Cell) step(action string, direction string) { | |
// this method will be called for each step of the game | |
// during this step current Cell has a right to move and to divide one time | |
if action == "move"{ | |
if direction == "left"{ | |
e.move(e.X-1, e.Y) | |
} else if direction == "right"{ | |
e.move(e.X+1, e.Y) | |
} else if direction == "top"{ | |
e.move(e.X, e.Y+1) | |
} else if direction == "bottom"{ | |
e.move(e.X, e.Y-1) | |
} | |
} else if action == "divide"{ | |
e.divide(direction) | |
} else if action == "devour"{ | |
if direction == "left"{ | |
e.devour(ArCells[find(e.X-1, e.Y)]) | |
} else if direction == "right"{ | |
e.devour(ArCells[find(e.X+1, e.Y)]) | |
} else if direction == "top"{ | |
e.devour(ArCells[find(e.X, e.Y+1)]) | |
} else if direction == "bottom"{ | |
e.devour(ArCells[find(e.X, e.Y-1)]) | |
} | |
} | |
// INSERT YOUR CODE HERE ####### | |
} | |
func find(x int, y int) int{ | |
var i int | |
for i = 0;i<len(ArCells);i++{ | |
if ArCells[i].X == x && ArCells[i].Y == y{ | |
break | |
} | |
} | |
return i | |
} | |
func (e Cell)remove() { | |
// Removes the cell from ArCells | |
var i int | |
for i=0; i< len(ArCells); i++ { | |
if ArCells[i] == e { | |
ArCells = append(ArCells[:i], ArCells[i+1:]...) | |
return //break | |
} | |
} | |
return | |
} | |
var BattleField [100][100]int // global battlefield | |
var ArCells []Cell // global Cells list | |
func drawGrid(field [100][100]int) { | |
var i, j int | |
for i = 0;i<100;i++ { | |
for j = 0; j<100; j++ { | |
fmt.Printf("%d ", field[i][j]) | |
} | |
fmt.Println() | |
} | |
} | |
func get_moves(cells []Cell) [][]string{ | |
var i int | |
var total_moves [][]string | |
for i=0;i<len(ArCells);i++ { | |
var moves []string | |
if cells[i].X - 1 >=0{ | |
if BattleField[cells[i].X - 1 ][cells[i].Y] == 0{ | |
moves = append(moves, "left") | |
} | |
} | |
if cells[i].X + 1 <100{ | |
if BattleField[cells[i].X + 1 ][cells[i].Y] == 0{ | |
moves = append(moves, "right") | |
} | |
} | |
if cells[i].Y + 1 < 100{ | |
if BattleField[cells[i].X ][cells[i].Y + 1] == 0{ | |
moves = append(moves, "top") | |
} | |
} | |
if cells[i].Y - 1 >=0{ | |
if BattleField[cells[i].X][cells[i].Y - 1] == 0{ | |
moves = append(moves, "bottom") | |
} | |
} | |
total_moves = append(total_moves, moves) | |
} | |
return total_moves | |
} | |
func best_move(moves [][]string) { | |
// Gets the best move based on the number of moves | |
var i, j int | |
var index int | |
var best string | |
var best_count int | |
best = "none" | |
index = -1 | |
best_count = 0 | |
for i=0;i<len(moves);i++{ | |
var cells []Cell | |
cells = ArCells | |
for j=0;j<len(moves[i]);j++{ | |
cells[i].step("move", moves[i][j]) | |
var future_moves [][]string | |
future_moves = get_moves(cells) | |
if size(future_moves) > best_count{ | |
best_count = size(future_moves) | |
index = i | |
best = moves[i][j] | |
} | |
} | |
} | |
ArCells[index].step("moves", best) | |
} | |
func size(matrix [][]string) int{ | |
var count,i int | |
count = 0 | |
for i=0;i<len(matrix);i++{ | |
count+=len(matrix[i]) | |
} | |
return count | |
} | |
func main() { | |
var i, j, probability int | |
// filling battlefield with space (0) and obstacles (1) | |
for i = 0; i < 100; i++ { | |
for j = 0; j < 100; j++ { | |
BattleField[i][j] = rand.Intn(1) | |
probability = rand.Intn(100) | |
if probability > 90 { | |
// we will add here the cells of other players | |
var cell Cell | |
cell = Cell{ | |
i, | |
j, | |
1, | |
0, | |
3} | |
ArCells = append(ArCells, cell) | |
BattleField[i][j] = cell.Team | |
} | |
} | |
} | |
// start battle ! | |
// INSERT YOUR CODE HERE ####### | |
var gameStatus bool | |
gameStatus = true | |
for gameStatus == true{ | |
var moves [][]string | |
moves = get_moves(ArCells) | |
best_move(moves) | |
if len(moves) == 0{ | |
gameStatus = false | |
} | |
//gameStatus = false | |
//drawGrid(BattleField | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment