Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: SharifAIChallenge/AIC20-Client-Python
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: v1.1.5
Choose a base ref
...
head repository: SharifAIChallenge/AIC20-Client-Python
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: master
Choose a head ref

Commits on Jan 9, 2020

  1. Merge branch 'master' of D:\Sharif\Events\AI Challenge 2020\aic20_cli…

    …ent_python with conflicts.
    taslimisina committed Jan 9, 2020
    Copy the full SHA
    4d5dd85 View commit details

Commits on Jan 28, 2020

  1. Merge remote-tracking branch 'origin/master'

    # Conflicts:
    #	world.py
    taslimisina committed Jan 28, 2020
    Copy the full SHA
    fc90340 View commit details

Commits on Jan 30, 2020

  1. Copy the full SHA
    7739412 View commit details

Commits on Feb 3, 2020

  1. Copy the full SHA
    b462f64 View commit details
  2. Copy the full SHA
    9d1e2a3 View commit details
  3. Copy the full SHA
    dadeb79 View commit details
  4. Normalized models

    taslimisina committed Feb 3, 2020
    Copy the full SHA
    6708e15 View commit details
  5. Copy the full SHA
    625493e View commit details
  6. Copy the full SHA
    06e66ab View commit details
  7. Copy the full SHA
    4552bfb View commit details

Commits on Feb 4, 2020

  1. Copy the full SHA
    f82ef02 View commit details
  2. Copy the full SHA
    a861dfd View commit details
  3. Nones handled

    HamidrezaKmK committed Feb 4, 2020
    Copy the full SHA
    76e8de9 View commit details
  4. cast unit spell changed

    HamidrezaKmK committed Feb 4, 2020
    Copy the full SHA
    efdedf4 View commit details
  5. choose deck test

    HamidrezaKmK committed Feb 4, 2020
    Copy the full SHA
    8622e3c View commit details
  6. Copy the full SHA
    ed79dc3 View commit details
  7. Copy the full SHA
    b7411f7 View commit details
  8. bug fix!

    HamidrezaKmK committed Feb 4, 2020
    Copy the full SHA
    df32ca8 View commit details
  9. client end added!

    HamidrezaKmK committed Feb 4, 2020
    Copy the full SHA
    7baf803 View commit details

Commits on Feb 5, 2020

  1. Copy the full SHA
    251db26 View commit details

Commits on Feb 8, 2020

  1. shortest path updated

    erfanfi79 committed Feb 8, 2020
    Copy the full SHA
    c7e05bf View commit details
  2. player ap update

    HamidrezaKmK committed Feb 8, 2020
    Copy the full SHA
    dd57645 View commit details
  3. shortest path updated

    erfanfi79 committed Feb 8, 2020
    Copy the full SHA
    099c7e2 View commit details
  4. name fixed

    erfanfi79 committed Feb 8, 2020
    Copy the full SHA
    23e860b View commit details
  5. name fixed

    ai info added
    erfanfi79 committed Feb 8, 2020
    Copy the full SHA
    217b610 View commit details
  6. name fixed

    ai info added
    erfanfi79 committed Feb 8, 2020
    Copy the full SHA
    1f6c844 View commit details
  7. Copy the full SHA
    3dce3db View commit details

Commits on Feb 10, 2020

  1. name fixed

    ai info added
    erfanfi79 committed Feb 10, 2020
    Copy the full SHA
    8cb60bc View commit details

Commits on Feb 12, 2020

  1. shortest path fixed

    erfanfi79 committed Feb 12, 2020
    Copy the full SHA
    76a3590 View commit details
  2. remaining turns fixed

    erfanfi79 committed Feb 12, 2020
    Copy the full SHA
    d61257d View commit details
  3. remaining turns fixed

    erfanfi79 committed Feb 12, 2020
    Copy the full SHA
    5d19515 View commit details
  4. Copy the full SHA
    7acccad View commit details

Commits on Feb 13, 2020

  1. sample AI added!

    HamidrezaKmK committed Feb 13, 2020
    Copy the full SHA
    31ed6dc View commit details
  2. Merge remote-tracking branch 'origin/master'

    # Conflicts:
    #	AI.py
    HamidrezaKmK committed Feb 13, 2020
    Copy the full SHA
    6e86008 View commit details
  3. add README

    HamidrezaKmK committed Feb 13, 2020
    Copy the full SHA
    dc1a488 View commit details

Commits on Feb 14, 2020

  1. Copy the full SHA
    97fc77f View commit details
  2. useless func deleted

    erfanfi79 committed Feb 14, 2020
    Copy the full SHA
    6422a6b View commit details
  3. get_friend_by_id fixed

    erfanfi79 committed Feb 14, 2020
    Copy the full SHA
    ea7d650 View commit details
  4. get king by id added

    erfanfi79 committed Feb 14, 2020
    Copy the full SHA
    8d8dc8a View commit details
  5. Fix get_spell_by_id name

    taslimisina committed Feb 14, 2020
    Copy the full SHA
    11d2b9b View commit details
  6. Fix str bug

    taslimisina committed Feb 14, 2020
    Copy the full SHA
    d6e83fb View commit details
  7. AI updated!

    HamidrezaKmK committed Feb 14, 2020
    Copy the full SHA
    212f697 View commit details
  8. sample AI added!

    HamidrezaKmK committed Feb 14, 2020
    Copy the full SHA
    b1b026e View commit details
  9. Fix ai

    taslimisina committed Feb 14, 2020
    Copy the full SHA
    cc93f9b View commit details
  10. Copy the full SHA
    a4372db View commit details
  11. Copy the full SHA
    667ba73 View commit details
  12. cast spell bug removed

    AI bug removed
    HamidrezaKmK committed Feb 14, 2020
    Copy the full SHA
    b891a75 View commit details

Commits on Feb 16, 2020

  1. update README.md

    HamidrezaKmK authored Feb 16, 2020
    Copy the full SHA
    3c48fcc View commit details
  2. Update README.md

    HamidrezaKmK authored Feb 16, 2020
    Copy the full SHA
    1917cb1 View commit details
  3. Update README.md

    HamidrezaKmK authored Feb 16, 2020
    Copy the full SHA
    5047370 View commit details
Showing with 867 additions and 432 deletions.
  1. +74 −11 AI.py
  2. +49 −0 README.md
  3. +10 −2 controller.py
  4. +221 −73 model.py
  5. +513 −346 world.py
85 changes: 74 additions & 11 deletions AI.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,78 @@
import random

from model import *
from world import World


class AI:
def __init__(self):
self.rows = 0
self.cols = 0
self.path_for_my_units = None

# this function is called in the beginning for deck picking and pre process
def pick(self, world: World):
print("pick started!")
# pre process
map = world.get_map()
self.rows = map.row_num
self.cols = map.col_num

# choosing all flying units
all_base_units = world.get_all_base_units()
my_hand = [base_unit for base_unit in all_base_units if base_unit.is_flying]

# picking the chosen hand - rest of the hand will automatically be filled with random base_units
world.choose_hand(base_units=my_hand)
# other pre process
self.path_for_my_units = world.get_friend().paths_from_player[0]

# it is called every turn for doing process during the game
def turn(self, world: World):
print("turn started:", world.get_current_turn())

myself = world.get_me()
max_ap = world.get_game_constants().max_ap
# play all of hand once your ap reaches maximum. if ap runs out, putUnit doesn't do anything
if myself.ap == max_ap:
for base_unit in myself.hand:
world.put_unit(base_unit=base_unit, path=self.path_for_my_units)

def pick(self, world):
print("pick")
world.choose_deck([1, 2, 3, 4])
print(world.get_player_by_id(world.get_my_id()))
print(world.get_player_by_id(world.get_friend_id()))
for p in world.map.paths:
print(p)
print("------------")
# this code tries to cast the received spell
received_spell = world.get_received_spell()
if received_spell is not None:
if received_spell.is_area_spell():
if received_spell.target == SpellTarget.ENEMY:
enemy_units = world.get_first_enemy().units
if len(enemy_units) > 0:
world.cast_area_spell(center=enemy_units[0].cell, spell=received_spell)
elif received_spell.target == SpellTarget.ALLIED:
friend_units = world.get_friend().units
if len(friend_units) > 0:
world.cast_area_spell(center=friend_units[0].cell, spell=received_spell)
elif received_spell.target == SpellTarget.SELF:
my_units = myself.units
if len(my_units) > 0:
world.cast_area_spell(center=my_units[0].cell, spell=received_spell)
else:
my_units = myself.units
if len(my_units) > 0:
unit = my_units[0]
my_paths = myself.paths_from_player
path = my_paths[random.randint(0, len(my_paths) - 1)]
size = len(path.cells)
cell = path.cells[int((size - 1) / 2)]
world.cast_unit_spell(unit=unit, path=path, cell=cell, spell=received_spell)

print(world.get_path_to_friend(world.get_my_id()))
# this code tries to upgrade damage of first unit. in case there's no damage token, it tries to upgrade range
if len(myself.units) > 0:
unit = myself.units[0]
world.upgrade_unit_damage(unit=unit)
world.upgrade_unit_range(unit=unit)

def turn(self, world):
print("turn")
# it is called after the game ended and it does not affect the game.
# using this function you can access the result of the game.
# scores is a map from int to int which the key is player_id and value is player_score
def end(self, world: World, scores):
print("end started!")
print("My score:", scores[world.get_me().player_id])
49 changes: 49 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
# Requirements

## Build

After implementing your code in `AI.py`, you may need to run it.
Below are the instructions to build and execute the client project.

![AI](http://s7.picofile.com/file/8388348218/AI.JPG)

### Using Command Line

To fetch and build the project use the following commands:

```
cd <path to working directory>
git clone https://github.com/SharifAIChallenge/AIC20-Client-Python
cd AIC20-Client-Python
```

Now you have to run the server ".jar" file and then you should `controller.py` 4 times. Each time you run controller a new client connects to the game. You can implement different AIs for different clients and run their controllers seperately.

### Using Pycharm

You can download Jetbrains Pycharm from this [link](https://www.jetbrains.com/pycharm/download/).

This IDE allows quick and easier usage.

1) Run the server using the command line located below the page in Pycharm and enter the following code:
```
cd your_server_directory/
java -jar server_jar_file.jar
```

2) Run `controller.py` by right clicking and choosing `Run 'controller'` option:

![run_controller](http://s6.picofile.com/file/8388348318/Run_controller.JPG)

3) Allow parallel runs:
* Click on the configuration of python
* Choose `edit Configurations`
![edit_config](http://s7.picofile.com/file/8388348276/edit_configuration.JPG)
* Click the check box with label `Allow parallel run`
![allow_parallel](http://s7.picofile.com/file/8388348250/allow_parallel.JPG)
* run the controller again for four times

## Multiple codes

For multiple codes you can copy the rest of the client source code and simply change the `AI.py` file as you will. Then run the controller for each source code

12 changes: 10 additions & 2 deletions controller.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import os
import sys
import threading
import traceback
from queue import Queue
from threading import Thread

@@ -12,6 +13,7 @@


class Controller:

def __init__(self):
self.sending_flag = True
self.conf = {}
@@ -35,16 +37,22 @@ def handle_message(self, message):
new_world._handle_turn_message(message[ServerConstants.KEY_INFO])
threading.Thread(target=self.launch_on_thread, args=(self.client.turn, new_world)).start()


elif message[ServerConstants.KEY_TYPE] == ServerConstants.MESSAGE_TYPE_SHUTDOWN:
new_world = World(world=self.world)
new_world._handle_turn_message(message[ServerConstants.KEY_INFO]["turnMessage"])
scores_map = new_world._handle_end_message(message[ServerConstants.KEY_INFO]["scores"])
self.client.end(new_world, scores_map)
self.terminate()

def launch_on_thread(self, action, world):
try:
action(world)
except Exception as e:
print("Error in client:")
print(e)
world.queue.put(Message(type=ServerConstants.MESSAGE_TYPE_END_TURN, turn=world.current_turn, info={}))
traceback.print_exc()
# print(e)
world._queue.put(Message(type=ServerConstants.MESSAGE_TYPE_END_TURN, turn=world.get_current_turn(), info={}))

def start(self):
self.read_settings()
Loading