-
Notifications
You must be signed in to change notification settings - Fork 0
/
lib.rs
235 lines (206 loc) · 6.37 KB
/
lib.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
use anchor_lang::prelude::*;
declare_id!("Enq7EUNZshTBsTMDzimmv4dv1wC8jaZmm7wgrSH4UPsj");
#[program]
pub mod tic_tac_toe_anchor {
use super::*;
pub fn initialize(
ctx: Context<InitializeCtx>,
_loby_name: String,
required_amount: u64,
delay_slots: u64,
) -> Result<()> {
msg!("Initializing game");
let game_data = &mut ctx.accounts.game_data;
game_data.player_a = *ctx.accounts.player_a.to_account_info().key;
game_data.player_b = *ctx.accounts.player_b.to_account_info().key;
game_data.turn_a = true;
game_data.player_a_has_deposited = false;
game_data.player_b_has_deposited = false;
game_data.board = [[Symbol::SymbolEmpty; 3]; 3];
game_data.end_slot = Clock::get()?.slot + delay_slots;
game_data.required_amount = required_amount;
Ok(())
}
pub fn make_move(
ctx: Context<MakeMoveCtx>,
_loby_name: String,
row: u8,
col: u8,
) -> Result<()> {
let game_data = &mut ctx.accounts.game_data;
let player = &ctx.accounts.player;
let (current_player, deposited) = if game_data.turn_a {
(game_data.player_a, game_data.player_a_has_deposited)
} else {
(game_data.player_b, game_data.player_b_has_deposited)
};
if !deposited {
msg!("Player deposits");
let transfer_instruction = anchor_lang::solana_program::system_instruction::transfer(
&player.key(),
&game_data.key(),
game_data.required_amount,
);
anchor_lang::solana_program::program::invoke(
&transfer_instruction,
&[player.to_account_info(), game_data.to_account_info()],
)
.unwrap();
if game_data.turn_a {
game_data.player_a_has_deposited = true;
} else {
game_data.player_b_has_deposited = true;
}
}
require!(player.key == ¤t_player, CustomError::InvalidPlayer);
require!(
Clock::get()?.slot < game_data.end_slot,
CustomError::TimeoutReached
);
require!(row < 3 && col < 3, CustomError::InvalidPosition);
require!(
game_data.board[row as usize][col as usize] == Symbol::SymbolEmpty,
CustomError::CellOccupied
);
let player_symbol = if game_data.turn_a {
Symbol::SymbolX
} else {
Symbol::SymbolO
};
game_data.board[row as usize][col as usize] = player_symbol;
game_data.turn_a = !game_data.turn_a;
if check_winner(game_data.board) {
msg!("Winner");
let amount = game_data.required_amount * 2;
**player.to_account_info().try_borrow_mut_lamports()? += amount;
**game_data.to_account_info().try_borrow_mut_lamports()? -= amount;
}
Ok(())
}
pub fn timeout(ctx: Context<TimeoutCtx>, _loby_name: String) -> Result<()> {
let game_data = &mut ctx.accounts.game_data;
let player = &ctx.accounts.player;
let allowed_player = if game_data.turn_a {
game_data.player_b
} else {
game_data.player_a
};
require!(
Clock::get()?.slot >= game_data.end_slot,
CustomError::TimeoutNotReached
);
let amount = game_data.required_amount * 2;
**player.to_account_info().try_borrow_mut_lamports()? += amount;
**game_data.to_account_info().try_borrow_mut_lamports()? = amount;
Ok(())
}
}
fn check_winner(board: [[Symbol; 3]; 3]) -> bool {
// Check rows and columns
for i in 0..3 {
if board[i][0] != Symbol::SymbolEmpty
&& board[i][0] == board[i][1]
&& board[i][0] == board[i][2]
{
return true;
}
if board[0][i] != Symbol::SymbolEmpty
&& board[0][i] == board[1][i]
&& board[0][i] == board[2][i]
{
return true;
}
}
// Check diagonals
if board[0][0] != Symbol::SymbolEmpty
&& board[0][0] == board[1][1]
&& board[0][0] == board[2][2]
{
return true;
}
if board[0][2] != Symbol::SymbolEmpty
&& board[0][2] == board[1][1]
&& board[0][2] == board[2][0]
{
return true;
}
return false;
}
#[derive(
InitSpace, borsh::BorshSerialize, borsh::BorshDeserialize, Clone, Debug, PartialEq, Eq, Copy,
)]
pub enum Symbol {
SymbolX,
SymbolO,
SymbolEmpty,
}
#[account]
#[derive(InitSpace)]
pub struct GameData {
pub player_a: Pubkey,
pub player_b: Pubkey,
pub turn_a: bool,
pub player_a_has_deposited: bool,
pub player_b_has_deposited: bool,
pub board: [[Symbol; 3]; 3],
pub end_slot: u64,
pub required_amount: u64,
}
#[derive(Accounts)]
#[instruction(_loby_name: String)]
pub struct InitializeCtx<'info> {
#[account(mut)]
pub initializer: Signer<'info>,
#[account(
init,
payer = initializer,
seeds = [_loby_name.as_ref()],
bump,
space = 8 + GameData::INIT_SPACE
)]
pub game_data: Account<'info, GameData>,
pub player_a: SystemAccount<'info>,
pub player_b: SystemAccount<'info>,
pub system_program: Program<'info, System>,
}
#[derive(Accounts)]
#[instruction(_loby_name: String)]
pub struct MakeMoveCtx<'info> {
#[account(mut)]
pub player: Signer<'info>,
#[account(
mut,
seeds = [_loby_name.as_ref()],
bump,
)]
pub game_data: Account<'info, GameData>,
pub system_program: Program<'info, System>,
}
#[derive(Accounts)]
#[instruction(_loby_name: String)]
pub struct TimeoutCtx<'info> {
#[account(mut)]
pub player: Signer<'info>,
#[account(
mut,
seeds = [_loby_name.as_ref()],
bump,
)]
pub game_data: Account<'info, GameData>,
pub system_program: Program<'info, System>,
}
#[error_code]
pub enum CustomError {
#[msg("Invalid Player")]
InvalidPlayer,
#[msg("Invalid position, must be between 0 and 2")]
InvalidPosition,
#[msg("Cell is already occupied")]
CellOccupied,
#[msg("Invalid amount, must be greater than 0")]
InvalidAmount,
#[msg("The timeout slot was reached")]
TimeoutReached,
#[msg("The timeout slot was not reached")]
TimeoutNotReached,
}