Skip to content

Commit 997c1c1

Browse files
committed
fix: catch the cat description
1 parent 5b03f86 commit 997c1c1

File tree

18 files changed

+497
-1
lines changed

18 files changed

+497
-1
lines changed

docs/artificialintelligence/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,4 +35,4 @@ add_subdirectory(assignments/flocking)
3535
add_subdirectory(assignments/maze)
3636
add_subdirectory(assignments/life)
3737
add_subdirectory(assignments/rng)
38-
#add_subdirectory(assignments/catchthecat)
38+
add_subdirectory(assignments/catchthecat)
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
add_executable(ai-catchthecat simulator.cpp)
2+
3+
file(GLOB TEST_INPUT_FILES ${CMAKE_CURRENT_SOURCE_DIR}/tests/*.in)
4+
file(GLOB TEST_OUTPUT_FILES ${CMAKE_CURRENT_SOURCE_DIR}/tests/*.out)
5+
6+
add_custom_test(ai-catchthecat-test ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/ai-catchthecat "${TEST_INPUT_FILES}" "${TEST_OUTPUT_FILES}")
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#ifndef IAgent_h
2+
#define IAgent_h
3+
#include <vector>
4+
#include <utility>
5+
#include <string>
6+
7+
// NO NOT CHANGE THIS FILE
8+
struct IAgent {
9+
public:
10+
/**
11+
* @brief the agent implementation. the center of the world is {0,0}, top left is {-sideSize/2, -sideSize/2} and the bottom right is {sideSize/2, sideSize/2}.
12+
*
13+
* @param world the world as a vector of booleans. true means there is a wall, false means there is no wall. The vector is the linearization of the matrix of the world.
14+
* @param catPos the position of the cat in the world {x,y} relative to the center of the world.
15+
* @param sideSize the side size of the world. it will be always a square that follows the sequence of 4*i+1: 5, 9, 13, 17, 21, 25, 29, 33, 37, 41, ...
16+
*
17+
* @return the position to move to {x,y}. relative to the center of the world.
18+
*/
19+
virtual std::pair<int,int> move(const std::vector<bool>& world, std::pair<int,int> catPos, int sideSize ) = 0;
20+
};
21+
#endif
Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
# Catch the Cat
2+
3+
You are in charge of creating 2 agents that will be playing the game of [Catch the Cat](https://llerrah.com/cattrap.htm).
4+
5+
## Game rules
6+
7+
The game is played on a NxN board where N is an odd number that follows the sequence of `1+4*x` with `x` starnig from `1`: `5, 9, 13, 17, 21, 25, 29, 33, 37, 41, 45, ...`. The game starts with a cat in the center of the board, and it starts with some random blocks placed randomly.
8+
9+
The game is played in turns, where each player can move the cat or a catcher.
10+
11+
### Board
12+
13+
The board position follows `{x, y}` notation.
14+
15+
The center of the board is `{0,0}` and the board is a square with `N` cells on each side.
16+
17+
The board is a pointy top hexagon with the first line aligned to the left. Here goes an example of a 5x5 board indexes:
18+
19+
```
20+
/ \ / \ / \ / \ / \
21+
|-2-2|-1-2| 0-2| 1-2| 2-2|
22+
\ / \ / \ / \ / \ / \
23+
|-2-1|-1-1| 0-1| 1-1| 2-1|
24+
/ \ / \ / \ / \ / \ /
25+
|-2 0|-1 0| 0 0| 1 0| 2 0|
26+
\ / \ / \ / \ / \ / \
27+
|-2 1|-1 1| 0 1| 1 1| 2 1|
28+
/ \ / \ / \ / \ / \ /
29+
|-2 2|-1 2| 0 2| 1 2| 2 2|
30+
\ / \ / \ / \ / \ /
31+
```
32+
33+
### Moves
34+
35+
The Cat moves in any of the 6 immediate neighbors, but it cannot move to a blocked cell.
36+
37+
The Catcher moves by blocking a cell. A cell can be blocked only once each turn.
38+
39+
### Win condition
40+
41+
1. If the cat is surrounded by blocked cells in all 6 directions, it cannot move and the catcher wins.
42+
2. If the cat reaches a border cell, it wins.
43+
3. If the cat makes invalid moves, it loses. Invalid moves are:
44+
45+
- Move to a blocked cell;
46+
- Move to a cell that is not a neighbor;
47+
- Stay in the same cell;
48+
49+
4. The catcher makes invalid moves, it loses. Invalid moves are:
50+
51+
- Block an already blocked cell;
52+
- Block a cell outside the board;
53+
- Block a cell where the cat is;
54+
55+
## Competition
56+
57+
All students enrolled in the competition will submit both agents. The agents will play against each other, and the winner will be the one that wins the most games.
58+
59+
The points will be counted as how many moves each one does;
60+
61+
If Cat Wins:
62+
63+
- CatPoints: SideSize * SideSize/2 - CatMoves - K*CpuCatTime;
64+
- CatcherPoints: CatcherMoves - K*CpuCatcherTime;
65+
66+
If Catcher Wins:
67+
68+
- CatPoints: CatMoves - K*CpuCatTime;
69+
- CatcherPoints: SideSize * SideSize/2 - CatcherMoves - K*CpuCatherTime;
70+
71+
## How to participate:
72+
73+
I will create an automation that will use your agents to play against each other.
74+
75+
1. Place the interface [below](#iagenth) in a file called `IAgent.h` on the root of your repo;
76+
2. Agents are stateless. At every turn, the state of all classes everything will be reset.
77+
3. The classes should be named `Cat` and `Catcher`;
78+
4. The simulator will include `Cat.h` and `Catcher.h`, so you should have at least these two files;
79+
5. Both agents should inherit `IAgent.h` and include `#include "IAgent.h"`;
80+
6. All `.cpp` and `.h` files should be at the same directory level. Don't use subdirs;
81+
7. Your submission will be a zip containing only `.h` and `.cpp` files.
82+
8. Do not submit any file with a `main` function;
83+
84+
The reasoning is: I will create an automation for:
85+
86+
1. Receive your zip and version them for auditing purposes and diagnostics;
87+
2. Create a folder for your user if not created yet;
88+
3. Clear the folder and keep the executable;
89+
4. Unzip the contents of your submission into a folder with your username;
90+
5. Add a `main.cpp` for the simulator;
91+
6. Compile the whole folder into one executable named as your username. Only the last working subimission will be kept;
92+
93+
It will generate `N` executables that will be managed and called via terminal to generate the final report with points;
94+
95+
The report will be generated via another automation that will generate 100 initial states randomly. All agents from all students play against each other.
96+
97+
```
98+
executables = fetchAllExecutables()
99+
initialstates = generateRandomStates(100);
100+
foreach cat of executables{
101+
foreach catcher of executables {
102+
turnIsCat = true;
103+
foreach state of initialstate {
104+
while(nat have winner && correct output){
105+
if(turnIsCat)
106+
state = cat(state)
107+
else
108+
state = catcher(state)
109+
turnIsCat = !turnIsCat
110+
}
111+
generate partial report from current cat and catcher
112+
}
113+
}
114+
}
115+
compose final report of the run
116+
```
117+
118+
### IAgent.h
119+
120+
```cpp title="IAgent.h"
121+
#pragma once
122+
#include <vector>
123+
#include <utility>
124+
125+
// NO NOT CHANGE THIS FILE
126+
struct IAgent {
127+
public:
128+
/**
129+
* @brief the agent implementation. the center of the world is {0,0}, top left is {-sideSize/2, -sideSize/2} and the bottom right is {sideSize/2, sideSize/2}.
130+
*
131+
* @param world the world as a vector of booleans. true means there is a wall, false means there is no wall. The vector is the linearization of the matrix of the world.
132+
* @param catPos the position of the cat in the world {x,y} relative to the center of the world.
133+
* @param sideSize the side size of the world. it will be always a square that follows the sequence of 4*i+1: 5, 9, 13, 17, 21, 25, 29, 33, 37, 41, ...
134+
*
135+
* @return the position to move to {x,y}. relative to the center of the world.
136+
*/
137+
virtual std::pair<int,int> move(const std::vector<bool>& world, std::pair<int,int> catPos, int sideSize ) = 0;
138+
};
139+
```
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
// DO NOT SUBMIT THIS FILE
2+
// IMPROVE THIS SIMULATOR FOR YOUR OWN USE
3+
// this code is not well tested, use as entry point for your own simulator
4+
#include <iostream>
5+
#include <vector>
6+
#include "Cat.h"
7+
#include "Catcher.h"
8+
9+
void print(const std::vector<bool>& state, int sideSize, std::pair<int,int> catPos, const std::string& turn){
10+
std::cout << turn << " " << sideSize << " " << catPos.first << " " << catPos.second << std::endl;
11+
catPos.first += sideSize/2;
12+
catPos.second += sideSize/2;
13+
auto catPosIndex = catPos.second * sideSize + catPos.first;
14+
for(int y=0; y<sideSize; y++) {
15+
if (y % 2 == 1) std::cout << ' ';
16+
for (int x = 0; x < sideSize; x++) {
17+
if(y * sideSize + x == catPosIndex) {
18+
std::cout << 'C';
19+
} else
20+
std::cout << (state[y * sideSize + x] ? '#' : '.');
21+
if (x < sideSize - 1) std::cout << ' ';
22+
}
23+
std::cout << std::endl;
24+
}
25+
}
26+
27+
std::vector<bool> readBoard(int sideSize) {
28+
std::vector<bool> board;
29+
board.reserve(sideSize*sideSize);
30+
for(int i=0; i<sideSize*sideSize; i++) {
31+
char c;
32+
std::cin >> c;
33+
switch (c) {
34+
case '#':
35+
board.push_back(true);
36+
break;
37+
case '.':
38+
case 'C':
39+
board.push_back(false);
40+
break;
41+
default:
42+
i--;
43+
break;
44+
}
45+
}
46+
return board;
47+
}
48+
49+
int main() {
50+
std::string turn;
51+
int sideSize;
52+
int catX, catY;
53+
std::vector<bool> blocked;
54+
std::cin >> turn >> sideSize >> catX >> catY;
55+
blocked = readBoard(sideSize);
56+
// while(not win){ simulate; } // todo: create your own logic to test and simulate, check for win conditions etc.
57+
if(turn == "CAT"){
58+
Cat cat;
59+
auto catMove = cat.move(blocked, {catX, catY}, sideSize);
60+
print(blocked, sideSize, {catMove.first, catMove.second}, "CATCHER");
61+
} else if (turn == "CATCHER") {
62+
Catcher catcher;
63+
auto catcherMove = catcher.move(blocked, {catX, catY}, sideSize);
64+
blocked[(catcherMove.second + sideSize/2) * sideSize + catcherMove.first+sideSize/2] = true;
65+
print(blocked, sideSize, {catX, catY}, "CATCHER");
66+
}
67+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
CAT 5 0 0
2+
. . . . .
3+
. # # . .
4+
. # C . .
5+
. # # . .
6+
. . . . .
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
CATCHER 5 1 0
2+
. . . . .
3+
. # # . .
4+
. # . C .
5+
. # # . .
6+
. . . . .
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
CAT 5 0 0
2+
. . . . .
3+
. # # . .
4+
. # C # .
5+
. # . . .
6+
. . . . .
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
CATCHER 5 0 1
2+
. . . . .
3+
. # # . .
4+
. # . # .
5+
. # C . .
6+
. . . . .
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
CAT 5 0 0
2+
. . . . .
3+
. # # . .
4+
. # C # .
5+
. . # . .
6+
. . . . .
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
CATCHER 5 -1 1
2+
. . . . .
3+
. # # . .
4+
. # . # .
5+
. C # . .
6+
. . . . .
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
CAT 5 0 0
2+
. . . . .
3+
. # # . .
4+
. . C # .
5+
. # # . .
6+
. . . . .
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
CATCHER 5 -1 0
2+
. . . . .
3+
. # # . .
4+
. C . # .
5+
. # # . .
6+
. . . . .
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
CAT 5 0 0
2+
. . . . .
3+
. # # . .
4+
. . C # .
5+
. # # . .
6+
. . . . .
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
CATCHER 5 -1 0
2+
. . . . .
3+
. # # . .
4+
. C . # .
5+
. # # . .
6+
. . . . .
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
CAT 5 0 0
2+
. . . . .
3+
. . # . .
4+
. # C # .
5+
. # # . .
6+
. . . . .
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
CATCHER 5 -1 -1
2+
. . . . .
3+
. C # . .
4+
. # . # .
5+
. # # . .
6+
. . . . .

0 commit comments

Comments
 (0)