Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
T
tp5-connect4
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
sven.wikberg
tp5-connect4
Commits
81c01415
Commit
81c01415
authored
6 years ago
by
sven.wikberg
Browse files
Options
Downloads
Patches
Plain Diff
last check
parent
df6522f4
No related branches found
No related tags found
No related merge requests found
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
src/lib.rs
+22
-55
22 additions, 55 deletions
src/lib.rs
with
22 additions
and
55 deletions
src/lib.rs
+
22
−
55
View file @
81c01415
...
...
@@ -71,7 +71,7 @@ impl Connect4 {
}
for
i
in
1
..
self
.height
{
if
self
.board
[
i
][
x
]
!=
Connect4Case
::
Empty
{
// on place la nouelle piece au dessus de la plus haute piece de la colonne
// on place la nou
v
elle piece au dessus de la plus haute piece de la colonne
self
.board
[
i
-
1
][
x
]
=
if
self
.is_red_turn
()
{
Connect4Case
::
Red
}
else
{
...
...
@@ -107,7 +107,11 @@ impl Connect4 {
self
.turn_count
+=
1
;
println!
(
"This is {}'s turn !!"
,
if
self
.is_red_turn
()
{
RED_CHAR
}
else
{
YELLOW_CHAR
}
if
self
.is_red_turn
()
{
RED_CHAR
}
else
{
YELLOW_CHAR
}
);
if
self
.is_red_turn
()
||
self
.game_mode
==
1
{
let
mut
col
:
usize
;
...
...
@@ -134,8 +138,7 @@ impl Connect4 {
fn
get_ia1_play
(
&
self
)
->
usize
{
loop
{
let
col
=
rust_hepia_lib
::
gen
(
0
,
self
.width
as
i32
)
as
usize
;
let
last_emp
=
self
.get_last_empty
(
col
);
if
last_emp
.is_some
()
{
if
self
.board
[
0
][
col
]
==
Connect4Case
::
Empty
{
return
col
;
}
}
...
...
@@ -157,7 +160,8 @@ impl Connect4 {
}
}
}
if
block_col
.is_some
()
{
// c'est dans une option car il ne pourrait ne pas y avoir de case a bloqué
if
block_col
.is_some
()
{
// c'est dans une option car il ne pourrait ne pas y avoir de case a bloqué
return
block_col
.unwrap
();
}
else
{
return
self
.get_ia1_play
();
...
...
@@ -165,10 +169,17 @@ impl Connect4 {
}
fn
get_last_empty
(
&
self
,
col
:
usize
)
->
Option
<
usize
>
{
if
self
.board
[
self
.height
-
1
][
col
]
==
Connect4Case
::
Empty
{
return
Some
(
self
.height
-
1
);
}
if
self
.board
[
0
][
col
]
!=
Connect4Case
::
Empty
{
return
None
;
}
// récupère la position vide la plus basse d'une colonne
if
self
.board
[
self
.height
-
1
][
col
]
==
Connect4Case
::
Empty
{
return
Some
(
self
.height
-
1
);
}
if
self
.board
[
0
][
col
]
!=
Connect4Case
::
Empty
{
return
None
;
}
for
i
in
1
..
self
.height
{
if
self
.board
[
i
][
col
]
!=
Connect4Case
::
Empty
{
return
Some
(
i
-
1
);
}
if
self
.board
[
i
][
col
]
!=
Connect4Case
::
Empty
{
return
Some
(
i
-
1
);
}
}
None
}
...
...
@@ -214,52 +225,6 @@ impl Connect4 {
println!
();
}
/*fn is_won(&self) -> bool {
for y in 0..self.height {
for x in 0..self.width {
if self.board[y][x] != Connect4Case::Empty {
if x + 3 < WIDTH {
// verif horizontale
if self.board[y][x + 1] == self.board[y][x]
&& self.board[y][x + 2] == self.board[y][x]
&& self.board[y][x + 3] == self.board[y][x]
{
return true;
}
}
if y + 3 < HEIGHT {
// verif verticale
if self.board[y + 1][x] == self.board[y][x]
&& self.board[y + 2][x] == self.board[y][x]
&& self.board[y + 3][x] == self.board[y][x]
{
return true;
}
}
if x + 3 < WIDTH && y + 3 < HEIGHT {
// verif diagonale 1 : \
if self.board[y + 1][x + 1] == self.board[y][x]
&& self.board[y + 2][x + 2] == self.board[y][x]
&& self.board[y + 3][x + 3] == self.board[y][x]
{
return true;
}
}
if x >= 3 && y + 3 < HEIGHT {
// verif diagonale 2 : /
if self.board[y + 1][x - 1] == self.board[y][x]
&& self.board[y + 2][x - 2] == self.board[y][x]
&& self.board[y + 3][x - 3] == self.board[y][x]
{
return true;
}
}
}
}
}
return false;
}*/
fn
is_full
(
&
self
)
->
bool
{
if
self
.turn_count
>=
self
.get_max_turn
()
{
true
...
...
@@ -297,6 +262,7 @@ impl Connect4 {
}
fn
cnt_same_piece_line
(
&
self
,
x
:
usize
,
y
:
usize
,
c
:
Connect4Case
,
dir
:
u8
)
->
u32
{
//compte le nombre de piece qui se suivent sur une ligne a partir d'une position et d'un direction
let
i
:
u32
;
i
=
self
.cnt_same_piece_dir
(
x
,
y
,
c
,
dir
)
+
self
.cnt_same_piece_dir
(
x
,
y
,
c
,
(
dir
+
4
)
%
8
)
...
...
@@ -305,6 +271,7 @@ impl Connect4 {
}
fn
cnt_same_piece_dir
(
&
self
,
x
:
usize
,
y
:
usize
,
c
:
Connect4Case
,
dir
:
u8
)
->
u32
{
//compte le nombre de piece qui se suivent sur une direction a partir d'une position et d'un direction
match
dir
{
0
=>
if
y
!=
0
&&
self
.board
[
y
-
1
][
x
]
==
c
{
//north
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment