联系方式

  • QQ:99515681
  • 邮箱:99515681@qq.com
  • 工作时间:8:00-23:00
  • 微信:codinghelp

您当前位置:首页 >> C/C++编程C/C++编程

日期:2019-12-16 11:22

Lab8 – Greedy Snake Battle

Greedy snake is a well-known computer game. In this lab, you’ll try to write an AI

program for a modified greedy-snake battle. Compete with each other and have fun!

Figure 1. Classic Greedy Snake Game

1. Basic rules

The modified greedy-snake game will let two snakes to fight against each other in a 50-

by-50 field. The goal of a player is to kill the other’s snake and keep his snake alive. At

each tick, the snake goes one step forward in the direction he assigned before. Once a

snake goes across border, or it crashes his or the opponents’ tail, he automatically loses

and the opponent wins. If the head of the snake lays on the food, the snake body will be

one pixel longer. If the battle didn’t end within 500 ticks, the snake with longer body wins.

To enable the battle of two AIs, we limit the operating speed of a play is one action per

tick, and limit the direction to be LEFT, RIGHT, UP or DOWN.

Figure 2. Greedy Snake Battle Field

The snakes

2. Interface and usage

The lab will support a basic interface, including the class you need to implement (game_t),

a bug-free 2D vector class (vector2D) and the file main.cpp containing all the I/O

specifications and gaming requirements. The implementation of game_t should be

written in a single file (lab8.cpp) and you should link the main.cpp and lab8.cpp to get

your final executable file (and it’s more recommended to write your own main.cpp to test

your implementations because your implementations should be valid with any valid

main.cpp).

The prototype of the game_t class is shown below:

/* The class that you need to implement for the game, the functions may

be useful to you

*/

class game_t {

private:

std::vector<vector2D> snakeSelf, snakeOpp;

vector2D food;

int round{};

bool first;

public:

/* EFFECTS: Initialize the game board

* MODIFIES: this

*/

game_t(vector2D birthSelf, vector2D birthOpp, vector2D food, bool

first);

/* EFFECTS: Set the position of food

* MODIFIES: this->food

*/

void setFood(vector2D food);

/* EFFECTS: Get the position of food

*/

vector2D getFood() const;

/* EFFECTS: Make a move with corresponding direction

* MODIFIES: this->snakeSelf, this->food, this->round

* RETURNS: 0 - Game continues

1 - You win

2 - Opponent win

*/

int selfMove(direction_t dir);

/* EFFECTS: Update the opponent's position

* MODIFIES: this->snakeOpp, this->food, this->round

* RETURNS: 0 - Game continues

1 - You win

2 - Opponent win

*/

int oppMove(direction_t dir);

/* EFFECTS: Tell if the move is invalid *Only to snakeSelf

*/

bool moveValid(direction_t dir);

/* EFFECTS: Tell if the move will cause boundary cross

*/

bool moveCross(direction_t dir);

/* EFFECTS: Determine the best direction to go at the current situation

*/

direction_t judge();

};

In the game_t class, the snakes are represented by two STL vectors of vector2D (snakeSelf

and snakeOpp). The class also contains private member variables food, round and first (if

you are the first to make action).

You should realize all the functions listed in the prototype of the game_t class and you

may use these functions in your judge function as well.

The related files could be downloaded in lab8-related-files.zip.

3. How to upload

To attend this lab and get some score, you need to submit the code at the OJ server

http://202.182.106.242. You should login with your student ID and the password you

preferred to use to log in the OJ, then you could submit your code and compete with

other AIs in http://202.182.106.242/submission/submit. Note that when you upload the

codes, you only need to upload the contents in your lab8.cpp and we will automatically

link other supporting files.

Please test your codes before submitting. Any intentional or unintentional attack to the

server, including malicious modification to other files, allocating excessing memory are

strictly forbidden and will cause extremely heavy deduction.


版权所有:留学生编程辅导网 2020 All Rights Reserved 联系方式:QQ:99515681 微信:codinghelp 电子信箱:99515681@qq.com
免责声明:本站部分内容从网络整理而来,只供参考!如有版权问题可联系本站删除。 站长地图

python代写
微信客服:codinghelp