Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
B
bevprog_2_bead_3
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
Package registry
Container registry
Model registry
Operate
Environments
Terraform modules
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
Susa Márk
bevprog_2_bead_3
Commits
a542e6e1
Commit
a542e6e1
authored
May 18, 2024
by
Susa Márk
Browse files
Options
Downloads
Patches
Plain Diff
Upload New File
parent
5998d3e2
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
1.4.cpp
+128
-0
128 additions, 0 deletions
1.4.cpp
with
128 additions
and
0 deletions
1.4.cpp
0 → 100644
+
128
−
0
View file @
a542e6e1
#include
"amoebawidget.hpp"
#include
"graphics.hpp"
using
namespace
genv
;
AmoebaWidget
::
AmoebaWidget
(
Window
*
window
,
int
pos_x
,
int
pos_y
,
int
width
,
int
height
)
:
OSWidget
(
window
,
pos_x
,
pos_y
,
width
,
height
)
{
cellSize
=
(
width
-
20
)
/
pathSize
;
cellPadding
=
2
;
currentPlayer
=
0
;
gameStarted
=
false
;
gameEnded
=
false
;
for
(
int
i
=
0
;
i
<
pathSize
;
++
i
)
{
for
(
int
j
=
0
;
j
<
pathSize
;
++
j
)
{
path
[
i
][
j
]
=
'.'
;
}
}
startButton
=
new
Button
(
window
,
400
,
385
,
95
,
30
,
"START"
);
startButton
->
setOnClick
([
this
]()
{
gameStarted
=
true
;
delete
startButton
;
});
winnerButton
=
new
Button
(
window
,
370
,
50
,
160
,
30
,
""
);
}
void
AmoebaWidget
::
draw
()
const
{
if
(
!
gameStarted
)
{
startButton
->
draw
();
}
else
{
if
(
gameEnded
)
{
winnerButton
->
draw
();
}
else
{
gout
<<
move_to
(
400
,
385
)
<<
color
(
0
,
0
,
0
)
<<
box
(
100
,
40
);
for
(
int
i
=
0
;
i
<
pathSize
;
++
i
)
{
for
(
int
j
=
0
;
j
<
pathSize
;
++
j
)
{
int
x
=
_pos_x
+
j
*
(
cellSize
+
cellPadding
);
int
y
=
_pos_y
+
i
*
(
cellSize
+
cellPadding
);
gout
<<
move_to
(
x
,
y
)
<<
color
(
200
,
200
,
200
)
<<
box
(
cellSize
,
cellSize
);
if
(
path
[
i
][
j
]
==
'X'
)
{
gout
<<
color
(
255
,
0
,
0
);
gout
<<
move_to
(
x
+
cellSize
/
4
,
y
+
cellSize
/
4
)
<<
line
(
cellSize
/
2
,
cellSize
/
2
);
gout
<<
move_to
(
x
+
cellSize
/
4
,
y
+
cellSize
*
3
/
4
)
<<
line
(
cellSize
/
2
,
-
cellSize
/
2
);
}
else
if
(
path
[
i
][
j
]
==
'O'
)
{
gout
<<
color
(
0
,
0
,
255
);
gout
<<
move_to
(
x
+
cellSize
/
3
,
y
+
cellSize
/
4
)
<<
text
(
"O"
);
}
}
}
}
}
gout
<<
refresh
;
}
void
AmoebaWidget
::
handle
(
genv
::
event
ev
)
{
if
(
!
gameStarted
)
{
startButton
->
handle
(
ev
);
}
else
if
(
!
gameEnded
&&
ev
.
type
==
ev_mouse
&&
ev
.
button
==
btn_left
)
{
int
cellX
=
(
ev
.
pos_x
-
_pos_x
)
/
(
cellSize
+
cellPadding
);
int
cellY
=
(
ev
.
pos_y
-
_pos_y
)
/
(
cellSize
+
cellPadding
);
if
(
cellX
>=
0
&&
cellX
<
pathSize
&&
cellY
>=
0
&&
cellY
<
pathSize
&&
path
[
cellY
][
cellX
]
==
'.'
)
{
if
(
currentPlayer
==
0
)
{
path
[
cellY
][
cellX
]
=
'X'
;
}
else
{
path
[
cellY
][
cellX
]
=
'O'
;
}
currentPlayer
=
(
currentPlayer
+
1
)
%
2
;
draw
();
if
(
checkWin
())
{
gameEnded
=
true
;
int
playerNumber
;
if
(
currentPlayer
==
0
)
{
playerNumber
=
2
;
}
else
{
playerNumber
=
1
;
}
std
::
string
winnerLabel
=
"Player "
+
std
::
to_string
(
playerNumber
)
+
" nyert!"
;
winnerButton
->
setLabel
(
winnerLabel
);
}
else
{
bool
isFull
=
true
;
for
(
int
i
=
0
;
i
<
pathSize
&&
isFull
;
++
i
)
{
for
(
int
j
=
0
;
j
<
pathSize
;
++
j
)
{
if
(
path
[
i
][
j
]
==
'.'
)
{
isFull
=
false
;
break
;
}
}
}
if
(
isFull
)
{
gameEnded
=
true
;
winnerButton
->
setLabel
(
" Dontetlen :)"
);
}
}
}
}
}
bool
AmoebaWidget
::
checkWin
()
const
{
for
(
int
i
=
0
;
i
<
pathSize
;
++
i
)
{
for
(
int
j
=
0
;
j
<
pathSize
-
4
;
++
j
)
{
if
(
path
[
i
][
j
]
!=
'.'
&&
path
[
i
][
j
]
==
path
[
i
][
j
+
1
]
&&
path
[
i
][
j
]
==
path
[
i
][
j
+
2
]
&&
path
[
i
][
j
]
==
path
[
i
][
j
+
3
]
&&
path
[
i
][
j
]
==
path
[
i
][
j
+
4
])
{
return
true
;
}
if
(
path
[
j
][
i
]
!=
'.'
&&
path
[
j
][
i
]
==
path
[
j
+
1
][
i
]
&&
path
[
j
][
i
]
==
path
[
j
+
2
][
i
]
&&
path
[
j
][
i
]
==
path
[
j
+
3
][
i
]
&&
path
[
j
][
i
]
==
path
[
j
+
4
][
i
])
{
return
true
;
}
}
}
for
(
int
i
=
0
;
i
<
pathSize
-
4
;
++
i
)
{
for
(
int
j
=
0
;
j
<
pathSize
-
4
;
++
j
)
{
if
(
path
[
i
][
j
]
!=
'.'
&&
path
[
i
][
j
]
==
path
[
i
+
1
][
j
+
1
]
&&
path
[
i
][
j
]
==
path
[
i
+
2
][
j
+
2
]
&&
path
[
i
][
j
]
==
path
[
i
+
3
][
j
+
3
]
&&
path
[
i
][
j
]
==
path
[
i
+
4
][
j
+
4
])
{
return
true
;
}
if
(
path
[
i
][
j
+
4
]
!=
'.'
&&
path
[
i
][
j
+
4
]
==
path
[
i
+
1
][
j
+
3
]
&&
path
[
i
][
j
+
4
]
==
path
[
i
+
2
][
j
+
2
]
&&
path
[
i
][
j
+
4
]
==
path
[
i
+
3
][
j
+
1
]
&&
path
[
i
][
j
+
4
]
==
path
[
i
+
4
][
j
])
{
return
true
;
}
}
}
return
false
;
}
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