├── .gitattributes
├── .gitignore
├── .gitmodules
├── CMakeLists.txt
├── Chess.sln
├── LICENSE.txt
├── README.md
├── cmake
└── sdl2
│ ├── FindSDL2_image.cmake
│ └── FindSDL2main.cmake
├── res
├── Chess_bdt60.png
├── Chess_blt60.png
├── Chess_kdt60.png
├── Chess_klt60.png
├── Chess_ndt60.png
├── Chess_nlt60.png
├── Chess_pdt60.png
├── Chess_plt60.png
├── Chess_qdt60.png
├── Chess_qlt60.png
├── Chess_rdt60.png
└── Chess_rlt60.png
├── sdl2-config.cmake
├── sdl2-image-config.cmake
└── src
├── Bishop.cpp
├── Bishop.h
├── CMakeLists.txt
├── Chess.vcxproj
├── Chess.vcxproj.filters
├── Game.cpp
├── Game.h
├── King.cpp
├── King.h
├── Knight.cpp
├── Knight.h
├── Main.cpp
├── MainLoop.cpp
├── MainLoop.h
├── Pawn.cpp
├── Pawn.h
├── Piece.cpp
├── Piece.h
├── Queen.cpp
├── Queen.h
├── Rook.cpp
├── Rook.h
├── SDL2.dll
├── SDL_Handler.cpp
└── SDL_Handler.h
/.gitattributes:
--------------------------------------------------------------------------------
1 | ###############################################################################
2 | # Set default behavior to automatically normalize line endings.
3 | ###############################################################################
4 | * text=auto
5 |
6 | ###############################################################################
7 | # Set default behavior for command prompt diff.
8 | #
9 | # This is need for earlier builds of msysgit that does not have it on by
10 | # default for csharp files.
11 | # Note: This is only used by command line
12 | ###############################################################################
13 | #*.cs diff=csharp
14 |
15 | ###############################################################################
16 | # Set the merge driver for project and solution files
17 | #
18 | # Merging from the command prompt will add diff markers to the files if there
19 | # are conflicts (Merging from VS is not affected by the settings below, in VS
20 | # the diff markers are never inserted). Diff markers may cause the following
21 | # file extensions to fail to load in VS. An alternative would be to treat
22 | # these files as binary and thus will always conflict and require user
23 | # intervention with every merge. To do so, just uncomment the entries below
24 | ###############################################################################
25 | #*.sln merge=binary
26 | #*.csproj merge=binary
27 | #*.vbproj merge=binary
28 | #*.vcxproj merge=binary
29 | #*.vcproj merge=binary
30 | #*.dbproj merge=binary
31 | #*.fsproj merge=binary
32 | #*.lsproj merge=binary
33 | #*.wixproj merge=binary
34 | #*.modelproj merge=binary
35 | #*.sqlproj merge=binary
36 | #*.wwaproj merge=binary
37 |
38 | ###############################################################################
39 | # behavior for image files
40 | #
41 | # image files are treated as binary by default.
42 | ###############################################################################
43 | #*.jpg binary
44 | #*.png binary
45 | #*.gif binary
46 |
47 | ###############################################################################
48 | # diff behavior for common document formats
49 | #
50 | # Convert binary document formats to text before diffing them. This feature
51 | # is only available from the command line. Turn it on by uncommenting the
52 | # entries below.
53 | ###############################################################################
54 | #*.doc diff=astextplain
55 | #*.DOC diff=astextplain
56 | #*.docx diff=astextplain
57 | #*.DOCX diff=astextplain
58 | #*.dot diff=astextplain
59 | #*.DOT diff=astextplain
60 | #*.pdf diff=astextplain
61 | #*.PDF diff=astextplain
62 | #*.rtf diff=astextplain
63 | #*.RTF diff=astextplain
64 |
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | ## Ignore Visual Studio temporary files, build results, and
2 | ## files generated by popular Visual Studio add-ons.
3 | ##
4 | ## Get latest from https://github.com/github/gitignore/blob/master/VisualStudio.gitignore
5 |
6 | # User-specific files
7 | *.rsuser
8 | *.suo
9 | *.user
10 | *.userosscache
11 | *.sln.docstates
12 |
13 | # User-specific files (MonoDevelop/Xamarin Studio)
14 | *.userprefs
15 |
16 | # Mono auto generated files
17 | mono_crash.*
18 |
19 | # Build results
20 | [Dd]ebug/
21 | [Dd]ebugPublic/
22 | [Rr]elease/
23 | [Rr]eleases/
24 | x64/
25 | x86/
26 | [Ww][Ii][Nn]32/
27 | [Aa][Rr][Mm]/
28 | [Aa][Rr][Mm]64/
29 | bld/
30 | [Bb]in/
31 | [Oo]bj/
32 | [Oo]ut/
33 | [Ll]og/
34 | [Ll]ogs/
35 |
36 | # Visual Studio 2015/2017 cache/options directory
37 | .vs/
38 | # Uncomment if you have tasks that create the project's static files in wwwroot
39 | #wwwroot/
40 |
41 | # Visual Studio 2017 auto generated files
42 | Generated\ Files/
43 |
44 | # MSTest test Results
45 | [Tt]est[Rr]esult*/
46 | [Bb]uild[Ll]og.*
47 |
48 | # NUnit
49 | *.VisualState.xml
50 | TestResult.xml
51 | nunit-*.xml
52 |
53 | # Build Results of an ATL Project
54 | [Dd]ebugPS/
55 | [Rr]eleasePS/
56 | dlldata.c
57 |
58 | # Benchmark Results
59 | BenchmarkDotNet.Artifacts/
60 |
61 | # .NET Core
62 | project.lock.json
63 | project.fragment.lock.json
64 | artifacts/
65 |
66 | # ASP.NET Scaffolding
67 | ScaffoldingReadMe.txt
68 |
69 | # StyleCop
70 | StyleCopReport.xml
71 |
72 | # Files built by Visual Studio
73 | *_i.c
74 | *_p.c
75 | *_h.h
76 | *.ilk
77 | *.meta
78 | *.obj
79 | *.iobj
80 | *.pch
81 | *.pdb
82 | *.ipdb
83 | *.pgc
84 | *.pgd
85 | *.rsp
86 | *.sbr
87 | *.tlb
88 | *.tli
89 | *.tlh
90 | *.tmp
91 | *.tmp_proj
92 | *_wpftmp.csproj
93 | *.log
94 | *.vspscc
95 | *.vssscc
96 | .builds
97 | *.pidb
98 | *.svclog
99 | *.scc
100 |
101 | # Chutzpah Test files
102 | _Chutzpah*
103 |
104 | # Visual C++ cache files
105 | ipch/
106 | *.aps
107 | *.ncb
108 | *.opendb
109 | *.opensdf
110 | *.sdf
111 | *.cachefile
112 | *.VC.db
113 | *.VC.VC.opendb
114 |
115 | # Visual Studio profiler
116 | *.psess
117 | *.vsp
118 | *.vspx
119 | *.sap
120 |
121 | # Visual Studio Trace Files
122 | *.e2e
123 |
124 | # TFS 2012 Local Workspace
125 | $tf/
126 |
127 | # Guidance Automation Toolkit
128 | *.gpState
129 |
130 | # ReSharper is a .NET coding add-in
131 | _ReSharper*/
132 | *.[Rr]e[Ss]harper
133 | *.DotSettings.user
134 |
135 | # TeamCity is a build add-in
136 | _TeamCity*
137 |
138 | # DotCover is a Code Coverage Tool
139 | *.dotCover
140 |
141 | # AxoCover is a Code Coverage Tool
142 | .axoCover/*
143 | !.axoCover/settings.json
144 |
145 | # Coverlet is a free, cross platform Code Coverage Tool
146 | coverage*.json
147 | coverage*.xml
148 | coverage*.info
149 |
150 | # Visual Studio code coverage results
151 | *.coverage
152 | *.coveragexml
153 |
154 | # NCrunch
155 | _NCrunch_*
156 | .*crunch*.local.xml
157 | nCrunchTemp_*
158 |
159 | # MightyMoose
160 | *.mm.*
161 | AutoTest.Net/
162 |
163 | # Web workbench (sass)
164 | .sass-cache/
165 |
166 | # Installshield output folder
167 | [Ee]xpress/
168 |
169 | # DocProject is a documentation generator add-in
170 | DocProject/buildhelp/
171 | DocProject/Help/*.HxT
172 | DocProject/Help/*.HxC
173 | DocProject/Help/*.hhc
174 | DocProject/Help/*.hhk
175 | DocProject/Help/*.hhp
176 | DocProject/Help/Html2
177 | DocProject/Help/html
178 |
179 | # Click-Once directory
180 | publish/
181 |
182 | # Publish Web Output
183 | *.[Pp]ublish.xml
184 | *.azurePubxml
185 | # Note: Comment the next line if you want to checkin your web deploy settings,
186 | # but database connection strings (with potential passwords) will be unencrypted
187 | *.pubxml
188 | *.publishproj
189 |
190 | # Microsoft Azure Web App publish settings. Comment the next line if you want to
191 | # checkin your Azure Web App publish settings, but sensitive information contained
192 | # in these scripts will be unencrypted
193 | PublishScripts/
194 |
195 | # NuGet Packages
196 | *.nupkg
197 | # NuGet Symbol Packages
198 | *.snupkg
199 | # The packages folder can be ignored because of Package Restore
200 | **/[Pp]ackages/*
201 | # except build/, which is used as an MSBuild target.
202 | !**/[Pp]ackages/build/
203 | # Uncomment if necessary however generally it will be regenerated when needed
204 | #!**/[Pp]ackages/repositories.config
205 | # NuGet v3's project.json files produces more ignorable files
206 | *.nuget.props
207 | *.nuget.targets
208 |
209 | # Microsoft Azure Build Output
210 | csx/
211 | *.build.csdef
212 |
213 | # Microsoft Azure Emulator
214 | ecf/
215 | rcf/
216 |
217 | # Windows Store app package directories and files
218 | AppPackages/
219 | BundleArtifacts/
220 | Package.StoreAssociation.xml
221 | _pkginfo.txt
222 | *.appx
223 | *.appxbundle
224 | *.appxupload
225 |
226 | # Visual Studio cache files
227 | # files ending in .cache can be ignored
228 | *.[Cc]ache
229 | # but keep track of directories ending in .cache
230 | !?*.[Cc]ache/
231 |
232 | # Others
233 | ClientBin/
234 | ~$*
235 | *~
236 | *.dbmdl
237 | *.dbproj.schemaview
238 | *.jfm
239 | *.pfx
240 | *.publishsettings
241 | orleans.codegen.cs
242 |
243 | # Including strong name files can present a security risk
244 | # (https://github.com/github/gitignore/pull/2483#issue-259490424)
245 | #*.snk
246 |
247 | # Since there are multiple workflows, uncomment next line to ignore bower_components
248 | # (https://github.com/github/gitignore/pull/1529#issuecomment-104372622)
249 | #bower_components/
250 |
251 | # RIA/Silverlight projects
252 | Generated_Code/
253 |
254 | # Backup & report files from converting an old project file
255 | # to a newer Visual Studio version. Backup files are not needed,
256 | # because we have git ;-)
257 | _UpgradeReport_Files/
258 | Backup*/
259 | UpgradeLog*.XML
260 | UpgradeLog*.htm
261 | ServiceFabricBackup/
262 | *.rptproj.bak
263 |
264 | # SQL Server files
265 | *.mdf
266 | *.ldf
267 | *.ndf
268 |
269 | # Business Intelligence projects
270 | *.rdl.data
271 | *.bim.layout
272 | *.bim_*.settings
273 | *.rptproj.rsuser
274 | *- [Bb]ackup.rdl
275 | *- [Bb]ackup ([0-9]).rdl
276 | *- [Bb]ackup ([0-9][0-9]).rdl
277 |
278 | # Microsoft Fakes
279 | FakesAssemblies/
280 |
281 | # GhostDoc plugin setting file
282 | *.GhostDoc.xml
283 |
284 | # Node.js Tools for Visual Studio
285 | .ntvs_analysis.dat
286 | node_modules/
287 |
288 | # Visual Studio 6 build log
289 | *.plg
290 |
291 | # Visual Studio 6 workspace options file
292 | *.opt
293 |
294 | # Visual Studio 6 auto-generated workspace file (contains which files were open etc.)
295 | *.vbw
296 |
297 | # Visual Studio LightSwitch build output
298 | **/*.HTMLClient/GeneratedArtifacts
299 | **/*.DesktopClient/GeneratedArtifacts
300 | **/*.DesktopClient/ModelManifest.xml
301 | **/*.Server/GeneratedArtifacts
302 | **/*.Server/ModelManifest.xml
303 | _Pvt_Extensions
304 |
305 | # Paket dependency manager
306 | .paket/paket.exe
307 | paket-files/
308 |
309 | # FAKE - F# Make
310 | .fake/
311 |
312 | # CodeRush personal settings
313 | .cr/personal
314 |
315 | # Python Tools for Visual Studio (PTVS)
316 | __pycache__/
317 | *.pyc
318 |
319 | # Cake - Uncomment if you are using it
320 | # tools/**
321 | # !tools/packages.config
322 |
323 | # Tabs Studio
324 | *.tss
325 |
326 | # Telerik's JustMock configuration file
327 | *.jmconfig
328 |
329 | # BizTalk build output
330 | *.btp.cs
331 | *.btm.cs
332 | *.odx.cs
333 | *.xsd.cs
334 |
335 | # OpenCover UI analysis results
336 | OpenCover/
337 |
338 | # Azure Stream Analytics local run output
339 | ASALocalRun/
340 |
341 | # MSBuild Binary and Structured Log
342 | *.binlog
343 |
344 | # NVidia Nsight GPU debugger configuration file
345 | *.nvuser
346 |
347 | # MFractors (Xamarin productivity tool) working folder
348 | .mfractor/
349 |
350 | # Local History for Visual Studio
351 | .localhistory/
352 |
353 | # BeatPulse healthcheck temp database
354 | healthchecksdb
355 |
356 | # Backup folder for Package Reference Convert tool in Visual Studio 2017
357 | MigrationBackup/
358 |
359 | # Ionide (cross platform F# VS Code tools) working folder
360 | .ionide/
361 |
362 | # Fody - auto-generated XML schema
363 | FodyWeavers.xsd
--------------------------------------------------------------------------------
/.gitmodules:
--------------------------------------------------------------------------------
1 | [submodule "cmake/sdl2"]
2 | path = cmake/sdl2
3 | url = https://gitlab.com/aminosbh/sdl2-cmake-modules.git
4 | [submodule "cmake/sdl2e"]
5 | path = cmake/sdl2e
6 | url = https://github.com/aminosbh/sdl2-cmake-modules
7 |
--------------------------------------------------------------------------------
/CMakeLists.txt:
--------------------------------------------------------------------------------
1 | cmake_minimum_required(VERSION 3.10)
2 |
3 | project(Chess)
4 |
5 | set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}/cmake/sdl2")
6 |
7 | find_package(SDL2 REQUIRED)
8 | find_package(SDL2_image REQUIRED)
9 |
10 | include_directories(${SDL2_INCLUDE_DIRS})
11 | include_directories(${SDL2_IMAGE_INCLUDE_DIRS})
12 |
13 |
14 | set(copy_source_dir "${CMAKE_SOURCE_DIR}")
15 | set(copy_dest_dir "${CMAKE_BINARY_DIR}")
16 | set(copy_file_name "res")
17 |
18 | # Add sub-projects for editor and game
19 | add_subdirectory(src)
20 |
21 | file(COPY ${CMAKE_CURRENT_SOURCE_DIR}/res DESTINATION ${CMAKE_CURRENT_BINARY_DIR})
22 |
23 |
24 |
--------------------------------------------------------------------------------
/Chess.sln:
--------------------------------------------------------------------------------
1 |
2 | Microsoft Visual Studio Solution File, Format Version 12.00
3 | # Visual Studio Version 16
4 | VisualStudioVersion = 16.0.31313.79
5 | MinimumVisualStudioVersion = 10.0.40219.1
6 | Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "Chess", "src\Chess.vcxproj", "{B1384727-1460-4AB0-B5A8-EA94D8C42B00}"
7 | EndProject
8 | Global
9 | GlobalSection(SolutionConfigurationPlatforms) = preSolution
10 | Debug|x64 = Debug|x64
11 | Debug|x86 = Debug|x86
12 | Release|x64 = Release|x64
13 | Release|x86 = Release|x86
14 | EndGlobalSection
15 | GlobalSection(ProjectConfigurationPlatforms) = postSolution
16 | {B1384727-1460-4AB0-B5A8-EA94D8C42B00}.Debug|x64.ActiveCfg = Debug|x64
17 | {B1384727-1460-4AB0-B5A8-EA94D8C42B00}.Debug|x64.Build.0 = Debug|x64
18 | {B1384727-1460-4AB0-B5A8-EA94D8C42B00}.Debug|x86.ActiveCfg = Debug|Win32
19 | {B1384727-1460-4AB0-B5A8-EA94D8C42B00}.Debug|x86.Build.0 = Debug|Win32
20 | {B1384727-1460-4AB0-B5A8-EA94D8C42B00}.Release|x64.ActiveCfg = Release|x64
21 | {B1384727-1460-4AB0-B5A8-EA94D8C42B00}.Release|x64.Build.0 = Release|x64
22 | {B1384727-1460-4AB0-B5A8-EA94D8C42B00}.Release|x86.ActiveCfg = Release|Win32
23 | {B1384727-1460-4AB0-B5A8-EA94D8C42B00}.Release|x86.Build.0 = Release|Win32
24 | EndGlobalSection
25 | GlobalSection(SolutionProperties) = preSolution
26 | HideSolutionNode = FALSE
27 | EndGlobalSection
28 | GlobalSection(ExtensibilityGlobals) = postSolution
29 | SolutionGuid = {C6E3AFD7-648E-477D-B722-87E4CB3752EF}
30 | EndGlobalSection
31 | EndGlobal
32 |
--------------------------------------------------------------------------------
/LICENSE.txt:
--------------------------------------------------------------------------------
1 | MIT License
2 |
3 | Copyright (c) 2022, Julian Unland
4 |
5 | Permission is hereby granted, free of charge, to any person obtaining a copy
6 | of this software and associated documentation files (the "Software"), to deal
7 | in the Software without restriction, including without limitation the rights
8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 | copies of the Software, and to permit persons to whom the Software is
10 | furnished to do so, subject to the following conditions:
11 |
12 | The above copyright notice and this permission notice shall be included in all
13 | copies or substantial portions of the Software.
14 |
15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 | SOFTWARE.
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 |
Chess
2 |
3 | Description
4 |
5 | ## Links
6 |
7 | - [Repo](https://github.com/JuUnland/Chess " Repo")
8 |
9 | ## Screenshots
10 |
11 | 
12 |
13 | ## Built With
14 |
15 | - C++
16 | - Visual Studio 2019
17 | - SDL2
18 | - SDL2_Image
19 |
20 | ## Build process
21 |
22 | - Clone the repository
23 | - copy "sdl2-config.cmake" into your SDL2 folder, which can be places anywhere
24 | - copy "sdl2-image-config.cmake" into your SDL2_Image folder, which can be placed anywhere
25 | - type "mkdir build && cd build && cmake .."
26 | - (open Chess.sln in Visual Studio, right click on Chess in your project folder and do "set as starting project")
27 |
28 | ## Requirements
29 |
30 | - x64, c++ 17 (others should work too)
31 | - SDL2
32 | - SDL2_image
33 |
34 | ## Future Updates
35 |
36 | - kp digga
37 |
38 | ## Author
39 |
40 | **Julian Unland**
41 |
42 | ## 🤝 Support
43 |
44 | Contributions, issues, and feature requests are welcome!
45 |
46 | Give a ⭐️ if you like this project!
47 |
--------------------------------------------------------------------------------
/cmake/sdl2/FindSDL2_image.cmake:
--------------------------------------------------------------------------------
1 | if(NOT SDL2_IMAGE_INCLUDE_DIR AND SDL2IMAGE_INCLUDE_DIR)
2 | set(SDL2_IMAGE_INCLUDE_DIR ${SDL2IMAGE_INCLUDE_DIR} CACHE PATH "directory cache entry initialized from old variable name")
3 | endif()
4 | find_path(SDL2_IMAGE_INCLUDE_DIR SDL_image.h
5 | HINTS
6 | ENV SDL2IMAGEDIR
7 | ENV SDL2DIR
8 | ${SDL2_DIR}
9 | PATH_SUFFIXES SDL2
10 | # path suffixes to search inside ENV{SDL2DIR}
11 | include/SDL2 include
12 | )
13 |
14 | if(CMAKE_SIZEOF_VOID_P EQUAL 8)
15 | set(VC_LIB_PATH_SUFFIX lib/x64)
16 | else()
17 | set(VC_LIB_PATH_SUFFIX lib/x86)
18 | endif()
19 |
20 | if(NOT SDL2_IMAGE_LIBRARY AND SDL2IMAGE_LIBRARY)
21 | set(SDL2_IMAGE_LIBRARY ${SDL2IMAGE_LIBRARY} CACHE FILEPATH "file cache entry initialized from old variable name")
22 | endif()
23 | find_library(SDL2_IMAGE_LIBRARY
24 | NAMES SDL2_image
25 | HINTS
26 | ENV SDL2IMAGEDIR
27 | ENV SDL2DIR
28 | ${SDL2_DIR}
29 | PATH_SUFFIXES lib ${VC_LIB_PATH_SUFFIX}
30 | )
31 |
32 | if(SDL2_IMAGE_INCLUDE_DIR AND EXISTS "${SDL2_IMAGE_INCLUDE_DIR}/SDL2_image.h")
33 | file(STRINGS "${SDL2_IMAGE_INCLUDE_DIR}/SDL2_image.h" SDL2_IMAGE_VERSION_MAJOR_LINE REGEX "^#define[ \t]+SDL2_IMAGE_MAJOR_VERSION[ \t]+[0-9]+$")
34 | file(STRINGS "${SDL2_IMAGE_INCLUDE_DIR}/SDL2_image.h" SDL2_IMAGE_VERSION_MINOR_LINE REGEX "^#define[ \t]+SDL2_IMAGE_MINOR_VERSION[ \t]+[0-9]+$")
35 | file(STRINGS "${SDL2_IMAGE_INCLUDE_DIR}/SDL2_image.h" SDL2_IMAGE_VERSION_PATCH_LINE REGEX "^#define[ \t]+SDL2_IMAGE_PATCHLEVEL[ \t]+[0-9]+$")
36 | string(REGEX REPLACE "^#define[ \t]+SDL2_IMAGE_MAJOR_VERSION[ \t]+([0-9]+)$" "\\1" SDL2_IMAGE_VERSION_MAJOR "${SDL2_IMAGE_VERSION_MAJOR_LINE}")
37 | string(REGEX REPLACE "^#define[ \t]+SDL2_IMAGE_MINOR_VERSION[ \t]+([0-9]+)$" "\\1" SDL2_IMAGE_VERSION_MINOR "${SDL2_IMAGE_VERSION_MINOR_LINE}")
38 | string(REGEX REPLACE "^#define[ \t]+SDL2_IMAGE_PATCHLEVEL[ \t]+([0-9]+)$" "\\1" SDL2_IMAGE_VERSION_PATCH "${SDL2_IMAGE_VERSION_PATCH_LINE}")
39 | set(SDL2_IMAGE_VERSION_STRING ${SDL2_IMAGE_VERSION_MAJOR}.${SDL2_IMAGE_VERSION_MINOR}.${SDL2_IMAGE_VERSION_PATCH})
40 | unset(SDL2_IMAGE_VERSION_MAJOR_LINE)
41 | unset(SDL2_IMAGE_VERSION_MINOR_LINE)
42 | unset(SDL2_IMAGE_VERSION_PATCH_LINE)
43 | unset(SDL2_IMAGE_VERSION_MAJOR)
44 | unset(SDL2_IMAGE_VERSION_MINOR)
45 | unset(SDL2_IMAGE_VERSION_PATCH)
46 | endif()
47 |
48 | set(SDL2_IMAGE_LIBRARIES ${SDL2_IMAGE_LIBRARY})
49 | set(SDL2_IMAGE_INCLUDE_DIRS ${SDL2_IMAGE_INCLUDE_DIR})
50 |
51 | include(FindPackageHandleStandardArgs)
52 |
53 | FIND_PACKAGE_HANDLE_STANDARD_ARGS(SDL2_image
54 | REQUIRED_VARS SDL2_IMAGE_LIBRARIES SDL2_IMAGE_INCLUDE_DIRS
55 | VERSION_VAR SDL2_IMAGE_VERSION_STRING)
56 |
57 | # for backward compatibility
58 | set(SDL2IMAGE_LIBRARY ${SDL2_IMAGE_LIBRARIES})
59 | set(SDL2IMAGE_INCLUDE_DIR ${SDL2_IMAGE_INCLUDE_DIRS})
60 | set(SDL2IMAGE_FOUND ${SDL2_IMAGE_FOUND})
61 |
62 | mark_as_advanced(SDL2_IMAGE_LIBRARY SDL2_IMAGE_INCLUDE_DIR)
--------------------------------------------------------------------------------
/cmake/sdl2/FindSDL2main.cmake:
--------------------------------------------------------------------------------
1 | if(NOT SDL2_DIR)
2 | set(SDL2_DIR "" CACHE PATH "SDL2 directory")
3 | endif()
4 |
5 | find_path(SDL2_INCLUDE_DIR SDL.h
6 | HINTS
7 | ENV SDL2DIR
8 | ${SDL2_DIR}
9 | PATH_SUFFIXES SDL2
10 | # path suffixes to search inside ENV{SDL2DIR}
11 | include/SDL2 include
12 | )
13 |
14 | if(CMAKE_SIZEOF_VOID_P EQUAL 8)
15 | set(VC_LIB_PATH_SUFFIX lib/x64)
16 | else()
17 | set(VC_LIB_PATH_SUFFIX lib/x86)
18 | endif()
19 |
20 | find_library(SDL2_LIBRARY_TEMP
21 | NAMES SDL2
22 | HINTS
23 | ENV SDL2DIR
24 | ${SDL2_DIR}
25 | PATH_SUFFIXES lib ${VC_LIB_PATH_SUFFIX}
26 | )
27 |
28 | # Hide this cache variable from the user, it's an internal implementation
29 | # detail. The documented library variable for the user is SDL2_LIBRARY
30 | # which is derived from SDL2_LIBRARY_TEMP further below.
31 | set_property(CACHE SDL2_LIBRARY_TEMP PROPERTY TYPE INTERNAL)
32 |
33 | if(NOT SDL2_BUILDING_LIBRARY)
34 | if(NOT SDL2_INCLUDE_DIR MATCHES ".framework")
35 | # Non-OS X framework versions expect you to also dynamically link to
36 | # SDLmain. This is mainly for Windows and OS X. Other (Unix) platforms
37 | # seem to provide SDLmain for compatibility even though they don't
38 | # necessarily need it.
39 | find_library(SDL2MAIN_LIBRARY
40 | NAMES SDL2main
41 | HINTS
42 | ENV SDL2DIR
43 | ${SDL2_DIR}
44 | PATH_SUFFIXES lib ${VC_LIB_PATH_SUFFIX}
45 | PATHS
46 | /sw
47 | /opt/local
48 | /opt/csw
49 | /opt
50 | )
51 | endif()
52 | endif()
53 |
54 | # SDL may require threads on your system.
55 | # The Apple build may not need an explicit flag because one of the
56 | # frameworks may already provide it.
57 | # But for non-OSX systems, I will use the CMake Threads package.
58 | if(NOT APPLE)
59 | find_package(Threads)
60 | endif()
61 |
62 | # MinGW needs an additional link flag, -mwindows
63 | # It's total link flags should look like -lmingw32 -lSDLmain -lSDL -mwindows
64 | if(MINGW)
65 | set(MINGW32_LIBRARY mingw32 "-mwindows" CACHE STRING "link flags for MinGW")
66 | endif()
67 |
68 | if(SDL2_LIBRARY_TEMP)
69 | # For SDLmain
70 | if(SDL2MAIN_LIBRARY AND NOT SDL2_BUILDING_LIBRARY)
71 | list(FIND SDL2_LIBRARY_TEMP "${SDLMAIN_LIBRARY}" _SDL2_MAIN_INDEX)
72 | if(_SDL2_MAIN_INDEX EQUAL -1)
73 | set(SDL2_LIBRARY_TEMP "${SDLMAIN_LIBRARY}" ${SDL2_LIBRARY_TEMP})
74 | endif()
75 | unset(_SDL2_MAIN_INDEX)
76 | endif()
77 |
78 | # For OS X, SDL uses Cocoa as a backend so it must link to Cocoa.
79 | # CMake doesn't display the -framework Cocoa string in the UI even
80 | # though it actually is there if I modify a pre-used variable.
81 | # I think it has something to do with the CACHE STRING.
82 | # So I use a temporary variable until the end so I can set the
83 | # "real" variable in one-shot.
84 | if(APPLE)
85 | set(SDL2_LIBRARY_TEMP ${SDL2_LIBRARY_TEMP} "-framework Cocoa")
86 | endif()
87 |
88 | # For threads, as mentioned Apple doesn't need this.
89 | # In fact, there seems to be a problem if I used the Threads package
90 | # and try using this line, so I'm just skipping it entirely for OS X.
91 | if(NOT APPLE)
92 | set(SDL2_LIBRARY_TEMP ${SDL2_LIBRARY_TEMP} ${CMAKE_THREAD_LIBS_INIT})
93 | endif()
94 |
95 | # For MinGW library
96 | if(MINGW)
97 | set(SDL2_LIBRARY_TEMP ${MINGW32_LIBRARY} ${SDL2_LIBRARY_TEMP})
98 | endif()
99 |
100 | # Set the final string here so the GUI reflects the final state.
101 | set(SDL2_LIBRARY ${SDL2_LIBRARY_TEMP} CACHE STRING "Where the SDL Library can be found")
102 | endif()
103 |
104 | if(SDL2_INCLUDE_DIR AND EXISTS "${SDL2_INCLUDE_DIR}/SDL2_version.h")
105 | file(STRINGS "${SDL2_INCLUDE_DIR}/SDL2_version.h" SDL2_VERSION_MAJOR_LINE REGEX "^#define[ \t]+SDL2_MAJOR_VERSION[ \t]+[0-9]+$")
106 | file(STRINGS "${SDL2_INCLUDE_DIR}/SDL2_version.h" SDL2_VERSION_MINOR_LINE REGEX "^#define[ \t]+SDL2_MINOR_VERSION[ \t]+[0-9]+$")
107 | file(STRINGS "${SDL2_INCLUDE_DIR}/SDL2_version.h" SDL2_VERSION_PATCH_LINE REGEX "^#define[ \t]+SDL2_PATCHLEVEL[ \t]+[0-9]+$")
108 | string(REGEX REPLACE "^#define[ \t]+SDL2_MAJOR_VERSION[ \t]+([0-9]+)$" "\\1" SDL2_VERSION_MAJOR "${SDL2_VERSION_MAJOR_LINE}")
109 | string(REGEX REPLACE "^#define[ \t]+SDL2_MINOR_VERSION[ \t]+([0-9]+)$" "\\1" SDL2_VERSION_MINOR "${SDL2_VERSION_MINOR_LINE}")
110 | string(REGEX REPLACE "^#define[ \t]+SDL2_PATCHLEVEL[ \t]+([0-9]+)$" "\\1" SDL2_VERSION_PATCH "${SDL2_VERSION_PATCH_LINE}")
111 | set(SDL2_VERSION_STRING ${SDL2_VERSION_MAJOR}.${SDL2_VERSION_MINOR}.${SDL2_VERSION_PATCH})
112 | unset(SDL2_VERSION_MAJOR_LINE)
113 | unset(SDL2_VERSION_MINOR_LINE)
114 | unset(SDL2_VERSION_PATCH_LINE)
115 | unset(SDL2_VERSION_MAJOR)
116 | unset(SDL2_VERSION_MINOR)
117 | unset(SDL2_VERSION_PATCH)
118 | endif()
119 |
120 | set(SDL2_LIBRARIES ${SDL2_LIBRARY} ${SDL2MAIN_LIBRARY})
121 | set(SDL2_INCLUDE_DIRS ${SDL2_INCLUDE_DIR})
122 |
123 | include(FindPackageHandleStandardArgs)
124 |
125 | FIND_PACKAGE_HANDLE_STANDARD_ARGS(SDL
126 | REQUIRED_VARS SDL2_LIBRARY SDL2_INCLUDE_DIR
127 | VERSION_VAR SDL2_VERSION_STRING)
--------------------------------------------------------------------------------
/res/Chess_bdt60.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/JuUnland/Chess/77c4951e370133d3342c731d3f6cf72312c4f684/res/Chess_bdt60.png
--------------------------------------------------------------------------------
/res/Chess_blt60.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/JuUnland/Chess/77c4951e370133d3342c731d3f6cf72312c4f684/res/Chess_blt60.png
--------------------------------------------------------------------------------
/res/Chess_kdt60.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/JuUnland/Chess/77c4951e370133d3342c731d3f6cf72312c4f684/res/Chess_kdt60.png
--------------------------------------------------------------------------------
/res/Chess_klt60.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/JuUnland/Chess/77c4951e370133d3342c731d3f6cf72312c4f684/res/Chess_klt60.png
--------------------------------------------------------------------------------
/res/Chess_ndt60.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/JuUnland/Chess/77c4951e370133d3342c731d3f6cf72312c4f684/res/Chess_ndt60.png
--------------------------------------------------------------------------------
/res/Chess_nlt60.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/JuUnland/Chess/77c4951e370133d3342c731d3f6cf72312c4f684/res/Chess_nlt60.png
--------------------------------------------------------------------------------
/res/Chess_pdt60.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/JuUnland/Chess/77c4951e370133d3342c731d3f6cf72312c4f684/res/Chess_pdt60.png
--------------------------------------------------------------------------------
/res/Chess_plt60.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/JuUnland/Chess/77c4951e370133d3342c731d3f6cf72312c4f684/res/Chess_plt60.png
--------------------------------------------------------------------------------
/res/Chess_qdt60.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/JuUnland/Chess/77c4951e370133d3342c731d3f6cf72312c4f684/res/Chess_qdt60.png
--------------------------------------------------------------------------------
/res/Chess_qlt60.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/JuUnland/Chess/77c4951e370133d3342c731d3f6cf72312c4f684/res/Chess_qlt60.png
--------------------------------------------------------------------------------
/res/Chess_rdt60.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/JuUnland/Chess/77c4951e370133d3342c731d3f6cf72312c4f684/res/Chess_rdt60.png
--------------------------------------------------------------------------------
/res/Chess_rlt60.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/JuUnland/Chess/77c4951e370133d3342c731d3f6cf72312c4f684/res/Chess_rlt60.png
--------------------------------------------------------------------------------
/sdl2-config.cmake:
--------------------------------------------------------------------------------
1 | set(SDL2_INCLUDE_DIRS "${CMAKE_CURRENT_LIST_DIR}/include")
2 |
3 | # Support both 32 and 64 bit builds
4 | if (${CMAKE_SIZEOF_VOID_P} MATCHES 8)
5 | set(SDL2_LIBRARIES "${CMAKE_CURRENT_LIST_DIR}/lib/x64/SDL2.lib;${CMAKE_CURRENT_LIST_DIR}/lib/x64/SDL2main.lib")
6 | else ()
7 | set(SDL2_LIBRARIES "${CMAKE_CURRENT_LIST_DIR}/lib/x86/SDL2.lib;${CMAKE_CURRENT_LIST_DIR}/lib/x86/SDL2main.lib")
8 | endif ()
9 |
10 | string(STRIP "${SDL2_LIBRARIES}" SDL2_LIBRARIES)
--------------------------------------------------------------------------------
/sdl2-image-config.cmake:
--------------------------------------------------------------------------------
1 | set(SDL2_IMAGE_DIRS "${CMAKE_CURRENT_LIST_DIR}/include")
2 |
3 | # Support both 32 and 64 bit builds
4 | if (${CMAKE_SIZEOF_VOID_P} MATCHES 8)
5 | set(SDL2_IMAGE_LIBRARIES "${CMAKE_CURRENT_LIST_DIR}/lib/x64/SDL2_image.lib")
6 | else ()
7 | set(SDL2_IMAGE_LIBRARIES "${CMAKE_CURRENT_LIST_DIR}/lib/x86/SDL2_image.lib")
8 | endif ()
9 |
10 | string(STRIP "${SDL2_IMAGE_LIBRARIES}" SDL2_IMAGE_LIBRARIES)
--------------------------------------------------------------------------------
/src/Bishop.cpp:
--------------------------------------------------------------------------------
1 | #include "Bishop.h"
2 | #include
3 |
4 | Bishop::Bishop(Team team, std::pair pos, SDL_Handler* handler)
5 | :Piece(team, pos, handler, BISHOP)
6 | {
7 | std::string filename;
8 | if (team == BLACK)
9 | {
10 | filename = "../res/Chess_bdt60.png";
11 | }
12 | else
13 | {
14 | filename = "../res/Chess_blt60.png";
15 | }
16 | m_handler = handler;
17 | m_texture = handler->loadImage(filename);
18 | render();
19 | }
20 |
21 | void Bishop::sayMyName()
22 | {
23 | if (m_team == BLACK)
24 | {
25 | std::cout << "BLACK BISHOP" << std::endl;
26 | }
27 | else
28 | {
29 | std::cout << "WHTIE BISHOP" << std::endl;
30 | }
31 | }
32 |
33 | void Bishop::calcPossibleMoves(Piece* field[8][8], bool checkCheck)
34 | {
35 | std::vector> moves;
36 | int dx_copy;
37 | int dy_copy;
38 | for (int dx = -1; dx <= 1; dx += 2)
39 | {
40 | for (int dy = -1; dy <= 1; dy += 2)
41 | {
42 | dx_copy = dx;
43 | dy_copy = dy;
44 | while (field[m_pos.first + dx_copy][m_pos.second + dy_copy] == nullptr
45 | && (m_pos.first + dx_copy >= 0 && m_pos.first + dx_copy <= 7 && m_pos.second + dy_copy >= 0 && m_pos.second + dy_copy <= 7))
46 | {
47 | moves = pushMove(moves,
48 | std::tuple(m_pos.first + dx_copy, m_pos.second + dy_copy, Piece::NORMAL),
49 | getOwnKing(field),
50 | field,
51 | checkCheck);
52 | if (dx_copy < 0)
53 | {
54 | dx_copy -= 1;
55 | }
56 | else
57 | {
58 | dx_copy += 1;
59 | }
60 | if (dy_copy < 0)
61 | {
62 | dy_copy -= 1;
63 | }
64 | else
65 | {
66 | dy_copy += 1;
67 | }
68 | }
69 | if (field[m_pos.first + dx_copy][m_pos.second + dy_copy] != nullptr
70 | && (m_pos.first + dx_copy >= 0 && m_pos.first + dx_copy <= 7 && m_pos.second + dy_copy >= 0 && m_pos.second + dy_copy <= 7))
71 | {
72 | if (field[m_pos.first + dx_copy][m_pos.second + dy_copy]->getTeam() != m_team)
73 | {
74 | moves = pushMove(moves,
75 | std::tuple(m_pos.first + dx_copy, m_pos.second + dy_copy, Piece::NORMAL),
76 | getOwnKing(field),
77 | field,
78 | checkCheck);
79 | }
80 | }
81 | }
82 | }
83 |
84 | m_possibleMoves = moves;
85 | }
86 |
87 |
88 |
89 |
--------------------------------------------------------------------------------
/src/Bishop.h:
--------------------------------------------------------------------------------
1 | #pragma once
2 | #include "Piece.h"
3 | #include "King.h"
4 | class Bishop :
5 | public Piece
6 | {
7 | public:
8 | //Constructor
9 | Bishop(Team team, std::pair pos, SDL_Handler* handler);
10 |
11 | // used to debug some stuff
12 | void sayMyName();
13 |
14 | // calculates the possible moves,
15 | void calcPossibleMoves(Piece* field[8][8], bool checkCheck);
16 | };
17 |
18 |
--------------------------------------------------------------------------------
/src/CMakeLists.txt:
--------------------------------------------------------------------------------
1 | # Setup sources
2 | set(CHESS_SOURCES
3 | Bishop.cpp
4 | Game.cpp
5 | King.cpp
6 | Knight.cpp
7 | mainLoop.cpp
8 | Pawn.cpp
9 | Piece.cpp
10 | Queen.cpp
11 | Rook.cpp
12 | SDL_Handler.cpp
13 | )
14 |
15 | file(COPY ${CMAKE_CURRENT_SOURCE_DIR}/SDL2.dll DESTINATION ${CMAKE_CURRENT_BINARY_DIR})
16 |
17 | # Build executable
18 | add_executable(Chess Main.cpp ${CHESS_SOURCES})
19 | target_link_libraries(Chess ${SDL2_LIBRARIES} ${SDL2_IMAGE_LIBRARIES})
--------------------------------------------------------------------------------
/src/Chess.vcxproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | Debug
6 | Win32
7 |
8 |
9 | Release
10 | Win32
11 |
12 |
13 | Debug
14 | x64
15 |
16 |
17 | Release
18 | x64
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 | 16.0
63 | Win32Proj
64 | {b1384727-1460-4ab0-b5a8-ea94d8c42b00}
65 | Chess
66 | 10.0
67 |
68 |
69 |
70 | Application
71 | true
72 | v142
73 | Unicode
74 |
75 |
76 | Application
77 | false
78 | v142
79 | true
80 | Unicode
81 |
82 |
83 | Application
84 | true
85 | v142
86 | Unicode
87 |
88 |
89 | Application
90 | false
91 | v142
92 | true
93 | Unicode
94 |
95 |
96 |
97 |
98 |
99 |
100 |
101 |
102 |
103 |
104 |
105 |
106 |
107 |
108 |
109 |
110 |
111 |
112 |
113 |
114 | true
115 |
116 |
117 | false
118 |
119 |
120 | true
121 | C:\Program Files %28x86%29\SDL2_image\include;C:\Program Files %28x86%29\SDL2\include;$(IncludePath)
122 | C:\Program Files %28x86%29\SDL2_image\lib\x64;C:\Program Files %28x86%29\SDL2\lib\x64;$(LibraryPath)
123 |
124 |
125 | false
126 |
127 |
128 |
129 | Level3
130 | true
131 | WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions)
132 | true
133 |
134 |
135 | Console
136 | true
137 |
138 |
139 |
140 |
141 | Level3
142 | true
143 | true
144 | true
145 | WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions)
146 | true
147 |
148 |
149 | Console
150 | true
151 | true
152 | true
153 |
154 |
155 |
156 |
157 | Level3
158 | true
159 | _DEBUG;_CONSOLE;%(PreprocessorDefinitions)
160 | true
161 | 26812;6835
162 | stdcpp17
163 | C:\Program Files %28x86%29\SDL2_image\include;C:\Program Files %28x86%29\SDL2\include;%(AdditionalIncludeDirectories)
164 |
165 |
166 | Console
167 | true
168 | SDL2.lib;SDL2main.lib;SDL2_image.lib;%(AdditionalDependencies)
169 | C:\Program Files %28x86%29\SDL2_image\lib;C:\Program Files %28x86%29\SDL2\lib\x64;%(AdditionalLibraryDirectories)
170 |
171 |
172 |
173 |
174 | Level3
175 | true
176 | true
177 | true
178 | NDEBUG;_CONSOLE;%(PreprocessorDefinitions)
179 | true
180 |
181 |
182 | Console
183 | true
184 | true
185 | true
186 |
187 |
188 |
189 |
190 |
191 |
--------------------------------------------------------------------------------
/src/Chess.vcxproj.filters:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | {4FC737F1-C7A5-4376-A066-2A32D752A2FF}
6 | cpp;c;cc;cxx;c++;cppm;ixx;def;odl;idl;hpj;bat;asm;asmx
7 |
8 |
9 | {93995380-89BD-4b04-88EB-625FBE52EBFB}
10 | h;hh;hpp;hxx;h++;hm;inl;inc;ipp;xsd
11 |
12 |
13 | {67DA6AB6-F800-4c08-8B7A-83BB121AAD01}
14 | rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms
15 |
16 |
17 |
18 |
19 | Quelldateien
20 |
21 |
22 | Quelldateien
23 |
24 |
25 | Quelldateien
26 |
27 |
28 | Quelldateien
29 |
30 |
31 | Quelldateien
32 |
33 |
34 | Quelldateien
35 |
36 |
37 | Quelldateien
38 |
39 |
40 | Quelldateien
41 |
42 |
43 | Quelldateien
44 |
45 |
46 | Quelldateien
47 |
48 |
49 | Quelldateien
50 |
51 |
52 |
53 |
54 | Headerdateien
55 |
56 |
57 | Headerdateien
58 |
59 |
60 | Headerdateien
61 |
62 |
63 | Headerdateien
64 |
65 |
66 | Headerdateien
67 |
68 |
69 | Headerdateien
70 |
71 |
72 | Headerdateien
73 |
74 |
75 | Headerdateien
76 |
77 |
78 | Headerdateien
79 |
80 |
81 | Headerdateien
82 |
83 |
84 |
85 |
86 | Ressourcendateien
87 |
88 |
89 | Ressourcendateien
90 |
91 |
92 | Ressourcendateien
93 |
94 |
95 | Ressourcendateien
96 |
97 |
98 | Ressourcendateien
99 |
100 |
101 | Ressourcendateien
102 |
103 |
104 | Ressourcendateien
105 |
106 |
107 | Ressourcendateien
108 |
109 |
110 | Ressourcendateien
111 |
112 |
113 | Ressourcendateien
114 |
115 |
116 | Ressourcendateien
117 |
118 |
119 | Ressourcendateien
120 |
121 |
122 | Ressourcendateien
123 |
124 |
125 |
--------------------------------------------------------------------------------
/src/Game.cpp:
--------------------------------------------------------------------------------
1 | #include "Game.h"
2 | #include
3 |
4 | Game::Game(SDL_Handler* handler)
5 | :pl1(new Pawn(Piece::WHITE, std::pair(0, 1), handler)),
6 | pl2(new Pawn(Piece::WHITE, std::pair(1, 1), handler)),
7 | pl3(new Pawn(Piece::WHITE, std::pair(2, 1), handler)),
8 | pl4(new Pawn(Piece::WHITE, std::pair(3, 1), handler)),
9 | pl5(new Pawn(Piece::WHITE, std::pair(4, 1), handler)),
10 | pl6(new Pawn(Piece::WHITE, std::pair(5, 1), handler)),
11 | pl7(new Pawn(Piece::WHITE, std::pair(6, 1), handler)),
12 | pl8(new Pawn(Piece::WHITE, std::pair(7, 1), handler)),
13 | pb1(new Pawn(Piece::BLACK, std::pair(0, 6), handler)),
14 | pb2(new Pawn(Piece::BLACK, std::pair(1, 6), handler)),
15 | pb3(new Pawn(Piece::BLACK, std::pair(2, 6), handler)),
16 | pb4(new Pawn(Piece::BLACK, std::pair(3, 6), handler)),
17 | pb5(new Pawn(Piece::BLACK, std::pair(4, 6), handler)),
18 | pb6(new Pawn(Piece::BLACK, std::pair(5, 6), handler)),
19 | pb7(new Pawn(Piece::BLACK, std::pair(6, 6), handler)),
20 | pb8(new Pawn(Piece::BLACK, std::pair(7, 6), handler)),
21 | rb1(new Rook(Piece::BLACK, std::pair(0, 7), handler)),
22 | rb2(new Rook(Piece::BLACK, std::pair(7, 7), handler)),
23 | rl1(new Rook(Piece::WHITE, std::pair(0, 0), handler)),
24 | rl2(new Rook(Piece::WHITE, std::pair(7, 0), handler)),
25 | nb1(new Knight(Piece::BLACK, std::pair(1, 7), handler)),
26 | nb2(new Knight(Piece::BLACK, std::pair(6, 7), handler)),
27 | nl1(new Knight(Piece::WHITE, std::pair(1, 0), handler)),
28 | nl2(new Knight(Piece::WHITE, std::pair(6, 0), handler)),
29 | bb1(new Bishop(Piece::BLACK, std::pair(2, 7), handler)),
30 | bb2(new Bishop(Piece::BLACK, std::pair(5, 7), handler)),
31 | bl1(new Bishop(Piece::WHITE, std::pair(2, 0), handler)),
32 | bl2(new Bishop(Piece::WHITE, std::pair(5, 0), handler)),
33 | kb1(new King(Piece::BLACK, std::pair(3, 7), handler)),
34 | kl1(new King(Piece::WHITE, std::pair(3, 0), handler)),
35 | qb1(new Queen(Piece::BLACK, std::pair(4, 7), handler)),
36 | ql1(new Queen(Piece::WHITE, std::pair(4, 0), handler)),
37 | m_turn(Piece::WHITE),
38 | m_handler(handler),
39 | m_checkEnPassant(true)
40 | {
41 | m_field[0][7] = rb1;
42 | m_field[7][7] = rb2;
43 | m_field[0][0] = rl1;
44 | m_field[7][0] = rl2;
45 |
46 | m_field[1][7] = nb1;
47 | m_field[6][7] = nb2;
48 | m_field[1][0] = nl1;
49 | m_field[6][0] = nl2;
50 |
51 | m_field[2][7] = bb1;
52 | m_field[5][7] = bb2;
53 | m_field[2][0] = bl1;
54 | m_field[5][0] = bl2;
55 |
56 | m_field[3][7] = kb1;
57 | m_field[3][0] = kl1;
58 |
59 | m_field[4][7] = qb1;
60 | m_field[4][0] = ql1;
61 |
62 | m_field[0][1] = pl1;
63 | m_field[1][1] = pl2;
64 | m_field[2][1] = pl3;
65 | m_field[3][1] = pl4;
66 | m_field[4][1] = pl5;
67 | m_field[5][1] = pl6;
68 | m_field[6][1] = pl7;
69 | m_field[7][1] = pl8;
70 |
71 | m_field[0][6] = pb1;
72 | m_field[1][6] = pb2;
73 | m_field[2][6] = pb3;
74 | m_field[3][6] = pb4;
75 | m_field[4][6] = pb5;
76 | m_field[5][6] = pb6;
77 | m_field[6][6] = pb7;
78 | m_field[7][6] = pb8;
79 |
80 | for (int i = 2; i < 6; i++)
81 | {
82 | for (int j = 0; j < 8; j++)
83 | {
84 | m_field[j][i] = nullptr;
85 | }
86 | }
87 |
88 | calcAllMoves();
89 | }
90 |
91 |
92 | Game::~Game()
93 | {
94 | }
95 |
96 |
97 | Piece* Game::getFieldPos(int row, int col)
98 | {
99 | return m_field[row][col];
100 | }
101 |
102 |
103 | void Game::move(Piece* start, std::tuple move)
104 | {
105 | if (m_checkEnPassant)
106 | {
107 | disableEnPassant();
108 | }
109 | else
110 | {
111 | m_checkEnPassant = true;
112 | }
113 |
114 | switch (std::get<2>(move))
115 | {
116 | case Piece::NORMAL:
117 | normal(start->getPos().first, start->getPos().second, std::get<0>(move), std::get<1>(move));
118 | break;
119 | case Piece::CASTLE:
120 | castles(start->getPos().first, start->getPos().second, std::get<0>(move), std::get<1>(move));
121 | break;
122 | case Piece::ENPASSANT:
123 | enPassant(start->getPos().first, start->getPos().second, std::get<0>(move), std::get<1>(move));
124 | break;
125 | case Piece::NEWPIECE:
126 | exchange(start->getPos().first, start->getPos().second, std::get<0>(move), std::get<1>(move));
127 | break;
128 | default:
129 | break;
130 | }
131 |
132 | gameState();
133 | }
134 |
135 |
136 | void Game::normal(int xStart, int yStart, int xEnd, int yEnd)
137 | {
138 | m_field[xEnd][yEnd] = getFieldPos(xStart, yStart);
139 | m_field[xEnd][yEnd]->m_hasMoved = true;
140 | m_field[xStart][yStart] = nullptr;
141 | m_handler->undoPieceRender(xStart, yStart);
142 | m_field[xEnd][yEnd]->setPosition(std::pair(xEnd, yEnd));
143 | if (m_field[xEnd][yEnd] != nullptr)
144 | {
145 | m_handler->undoPieceRender(xEnd, yEnd);
146 | }
147 | m_field[xEnd][yEnd]->render();
148 |
149 | if (m_field[xEnd][yEnd]->getType() == Piece::PAWN)
150 | {
151 | if (abs(yEnd - yStart) == 2)
152 | {
153 | if (xEnd - 1 >= 0)
154 | {
155 | if (m_field[xEnd - 1][yEnd] != nullptr)
156 | {
157 | if (m_field[xEnd - 1][yEnd]->getType() == Piece::PAWN)
158 | {
159 | Pawn* pwn = static_cast(m_field[xEnd - 1][yEnd]);
160 | pwn->setEnPassant(std::pair(true, -1));
161 | m_checkEnPassant = false;
162 | }
163 | }
164 | }
165 |
166 | if (xEnd + 1 <= 7)
167 | {
168 | if (m_field[xEnd + 1][yEnd] != nullptr)
169 | {
170 | if (m_field[xEnd + 1][yEnd]->getType() == Piece::PAWN)
171 | {
172 | Pawn* pwn = static_cast(m_field[xEnd + 1][yEnd]);
173 | pwn->setEnPassant(std::pair(true, 1));
174 | m_checkEnPassant = false;
175 | }
176 | }
177 | }
178 | }
179 | }
180 | }
181 |
182 |
183 | void Game::enPassant(int xStart, int yStart, int xEnd, int yEnd)
184 | {
185 | Pawn* pawn_start = static_cast(m_field[xStart][yStart]);
186 | m_field[xEnd][yEnd - pawn_start->m_dy] = nullptr;
187 | m_field[xEnd][yEnd] = getFieldPos(xStart, yStart);
188 | m_field[xEnd][yEnd]->m_hasMoved = true;
189 | m_field[xStart][yStart] = nullptr;
190 | m_handler->undoPieceRender(xStart, yStart);
191 | m_handler->undoPieceRender(xEnd, yEnd - pawn_start->m_dy);
192 | m_field[xEnd][yEnd]->setPosition(std::pair(xEnd, yEnd));
193 | m_field[xEnd][yEnd]->render();
194 | }
195 |
196 |
197 | void Game::exchange(int xStart, int yStart, int xEnd, int yEnd)
198 | {
199 | SDL_Texture* text_rook = m_handler->loadImage("../res/Chess_rlt60.png");
200 | SDL_Texture* text_knight = m_handler->loadImage("../res/Chess_nlt60.png");
201 | SDL_Texture* text_bishop = m_handler->loadImage("../res/Chess_blt60.png");
202 | SDL_Texture* text_queen = m_handler->loadImage("../res/Chess_qlt60.png");
203 | int y_draw = 0;
204 | Piece::Team team = Piece::WHITE;
205 |
206 | if (m_field[xStart][yStart]->getTeam() == Piece::BLACK)
207 | {
208 | text_rook = m_handler->loadImage("../res/Chess_rdt60.png");
209 | text_knight = m_handler->loadImage("../res/Chess_ndt60.png");
210 | text_bishop = m_handler->loadImage("../res/Chess_bdt60.png");
211 | text_queen = m_handler->loadImage("../res/Chess_qdt60.png");
212 | y_draw = 3 * m_handler->SCREEN_HEIGHT / 4;
213 | team = Piece::BLACK;
214 | }
215 |
216 | SDL_SetRenderDrawColor(m_handler->m_renderer, 155, 103, 60, 255);
217 | SDL_Rect rectangle = {0,
218 | y_draw,
219 | m_handler->SCREEN_WIDTH / 4,
220 | m_handler->SCREEN_HEIGHT / 4 };
221 | SDL_RenderFillRect(m_handler->m_renderer, &rectangle);
222 | SDL_Rect src = { 0, 0, 60, 60 };
223 | m_handler->DrawRectangle(src, rectangle, text_rook);
224 |
225 | SDL_SetRenderDrawColor(m_handler->m_renderer, 255, 255, 255, 255);
226 | rectangle.x = m_handler->SCREEN_WIDTH / 4;
227 | SDL_RenderFillRect(m_handler->m_renderer, &rectangle);
228 | m_handler->DrawRectangle(src, rectangle, text_knight);
229 |
230 | SDL_SetRenderDrawColor(m_handler->m_renderer, 155, 103, 60, 255);
231 | rectangle.x = 2 * m_handler->SCREEN_WIDTH / 4;
232 | SDL_RenderFillRect(m_handler->m_renderer, &rectangle);
233 | m_handler->DrawRectangle(src, rectangle, text_bishop);
234 |
235 | SDL_SetRenderDrawColor(m_handler->m_renderer, 255, 255, 255, 255);
236 | rectangle.x = 3 * m_handler->SCREEN_WIDTH / 4;
237 | SDL_RenderFillRect(m_handler->m_renderer, &rectangle);
238 | m_handler->DrawRectangle(src, rectangle, text_queen);
239 |
240 | bool quit = false;
241 | int x = -1;
242 | int y = -1;
243 |
244 | Piece* clickedOn = nullptr;
245 |
246 | std::cout << m_handler;
247 |
248 | while (quit == false)
249 | {
250 | while (SDL_PollEvent(&m_handler->m_event))
251 | {
252 | if (m_handler->m_event.type == SDL_QUIT)
253 | {
254 | quit = true;
255 | }
256 |
257 | if (m_handler->m_event.type == SDL_MOUSEBUTTONDOWN)
258 | {
259 | x = m_handler->m_event.button.x / 160;
260 | y = m_handler->m_event.button.y / 160;
261 |
262 | if (y >= y_draw / 160 && y < y_draw / 160 + 1)
263 | {
264 | if (x < m_handler->SCREEN_WIDTH / 640)
265 | {
266 | clickedOn = new Rook(team ,std::pair(xEnd, yEnd), m_handler);
267 | }
268 | else if (x < 2 * m_handler->SCREEN_WIDTH / 640)
269 | {
270 | clickedOn = new Knight(team ,std::pair(xEnd, yEnd), m_handler);
271 | }
272 | else if (x < 3 * m_handler->SCREEN_WIDTH / 640)
273 | {
274 | clickedOn = new Bishop(team ,std::pair(xEnd, yEnd), m_handler);
275 | }
276 | else if (x <= 4 * m_handler->SCREEN_WIDTH / 640)
277 | {
278 | clickedOn = new Queen(team ,std::pair(xEnd, yEnd), m_handler);
279 | }
280 | std::cout << x << " " << m_handler->SCREEN_WIDTH / 640 << std::endl;
281 | }
282 | }
283 |
284 | if (m_handler->m_event.type == SDL_MOUSEBUTTONUP && clickedOn != nullptr)
285 | {
286 | quit = true;
287 | }
288 | }
289 | }
290 |
291 | m_field[xEnd][yEnd] = clickedOn;
292 | m_field[xStart][yStart] = nullptr;
293 | m_handler->undoPieceRender(xStart, yStart);
294 | m_handler->renderBackground();
295 |
296 | for (int i = 0; i < 8; i++)
297 | {
298 | for (int j = 0; j < 8; j++)
299 | {
300 | if (m_field[i][j] != nullptr)
301 | {
302 | m_field[i][j]->render();
303 | }
304 | }
305 | }
306 |
307 | SDL_DestroyTexture(text_rook);
308 | SDL_DestroyTexture(text_bishop);
309 | SDL_DestroyTexture(text_knight);
310 | SDL_DestroyTexture(text_queen);
311 | }
312 |
313 |
314 | void Game::castles(int xStart, int yStart, int xEnd, int yEnd)
315 | {
316 | if (xEnd == 0)
317 | {
318 | m_field[2][yEnd] = m_field[4][yEnd];
319 | m_field[3][yEnd] = m_field[0][yEnd];
320 | m_field[2][yEnd]->m_hasMoved = true;
321 | m_field[3][yEnd]->m_hasMoved = true;
322 | m_field[2][yEnd]->setPosition(std::pair(2, yEnd));
323 | m_field[3][yEnd]->setPosition(std::pair(3, yEnd));
324 | m_field[4][yEnd] = nullptr;
325 | m_field[0][yEnd] = nullptr;
326 | m_handler->undoPieceRender(4, yEnd);
327 | m_handler->undoPieceRender(0, yEnd);
328 | m_field[2][yEnd]->render();
329 | m_field[3][yEnd]->render();
330 | }
331 | else
332 | {
333 | m_field[6][yEnd] = m_field[4][yEnd];
334 | m_field[5][yEnd] = m_field[7][yEnd];
335 | m_field[6][yEnd]->m_hasMoved = true;
336 | m_field[5][yEnd]->m_hasMoved = true;
337 | m_field[6][yEnd]->setPosition(std::pair(6, yEnd));
338 | m_field[5][yEnd]->setPosition(std::pair(5, yEnd));
339 | m_field[4][yEnd] = nullptr;
340 | m_field[7][yEnd] = nullptr;
341 | m_handler->undoPieceRender(4, yEnd);
342 | m_handler->undoPieceRender(7, yEnd);
343 | m_field[6][yEnd]->render();
344 | m_field[5][yEnd]->render();
345 | }
346 | }
347 |
348 | void Game::gameState()
349 | {
350 | bool lost = true;
351 | King* pivot = kb1;
352 |
353 | if (m_turn == Piece::BLACK)
354 | {
355 | pivot = kl1;
356 | }
357 |
358 | pivot->setCheck(m_field, kl1->getPos().first, kl1->getPos().second);
359 | for (int i = 0; i < 8; i++)
360 | {
361 | for (int j = 0; j < 8; j++)
362 | {
363 | if (m_field[i][j] != nullptr)
364 | {
365 | if (m_field[i][j]->getTeam() != m_turn)
366 | {
367 | m_field[i][j]->calcPossibleMoves(m_field, true);
368 | if (!m_field[i][j]->getPossibleMoves().empty())
369 | {
370 | lost = false;
371 | }
372 | }
373 | }
374 | }
375 | }
376 |
377 | if (pivot->getCheck() && lost)
378 | {
379 | if (m_turn == Piece::BLACK)
380 | {
381 | std::cout << "Black wins!";
382 | }
383 | else
384 | {
385 | std::cout << "White wins!";
386 | }
387 | }
388 | else if (lost)
389 | {
390 | if (m_turn == Piece::BLACK)
391 | {
392 | std::cout << "Remis!";
393 | }
394 | else
395 | {
396 | std::cout << "Remis!";
397 | }
398 | }
399 | if (m_turn == Piece::BLACK)
400 | {
401 | m_turn = Piece::WHITE;
402 | }
403 | else
404 | {
405 | m_turn = Piece::BLACK;
406 | }
407 |
408 | }
409 |
410 |
411 | void Game::disableEnPassant()
412 | {
413 | for (int i = 0; i < 8; i++)
414 | {
415 | for (int j = 0; j < 8; j++)
416 | {
417 | if (m_field[i][j] != nullptr)
418 | {
419 | if (m_field[i][j]->getType() == Piece::PAWN)
420 | {
421 | Pawn* pwn = static_cast(m_field[i][j]);
422 | pwn->setEnPassant(std::pair(false, 0));
423 | }
424 | }
425 | }
426 | }
427 | }
428 |
429 |
430 | void Game::renderPossibleMoves(Piece* piece)
431 | {
432 | piece->calcPossibleMoves(m_field, true);
433 | std::vector> possible = piece->getPossibleMoves();
434 | SDL_Rect rectangle;
435 | for (const auto& value : possible) {
436 | if ((std::get<0>(value) % 2 == 0 && std::get<1>(value) % 2 == 1) || (std::get<0>(value) % 2 == 1 && std::get<1>(value) % 2 == 0))
437 | {
438 | SDL_SetRenderDrawColor(m_handler->m_renderer, 0, 134, 139, 255);
439 | }
440 | else
441 | {
442 | SDL_SetRenderDrawColor(m_handler->m_renderer, 164, 211, 238, 255);
443 | }
444 | rectangle = { std::get<0>(value) * m_handler->SCREEN_WIDTH / 8,
445 | std::get<1>(value)* m_handler->SCREEN_HEIGHT / 8,
446 | m_handler->SCREEN_WIDTH / 8,
447 | m_handler->SCREEN_HEIGHT / 8 };
448 | SDL_RenderFillRect(m_handler->m_renderer, &rectangle);
449 |
450 | for (int i = 0; i < 8; i++)
451 | {
452 | for (int j = 0; j < 8; j++)
453 | {
454 | if (m_field[i][j] != nullptr)
455 | {
456 | m_field[i][j]->render();
457 | }
458 | }
459 | }
460 | }
461 | }
462 |
463 | void Game::undoRenderPossibleMoves(Piece* piece)
464 | {
465 | std::vector> possible = piece->getPossibleMoves();
466 | for (const auto& value : possible) {
467 | if ((std::get<0>(value) % 2 == 0 && std::get<1>(value) % 2 == 1) || (std::get<0>(value) % 2 == 1 && std::get<1>(value) % 2 == 0))
468 | {
469 | SDL_SetRenderDrawColor(m_handler->m_renderer, 155, 103, 60, 255);
470 | }
471 | else
472 | {
473 | SDL_SetRenderDrawColor(m_handler->m_renderer, 255, 255, 255, 255);
474 | }
475 | SDL_Rect rectangle = { std::get<0>(value) * m_handler->SCREEN_WIDTH / 8,
476 | std::get<1>(value) * m_handler->SCREEN_HEIGHT / 8,
477 | m_handler->SCREEN_WIDTH / 8,
478 | m_handler->SCREEN_HEIGHT / 8 };
479 | SDL_RenderFillRect(m_handler->m_renderer, &rectangle);
480 |
481 | for (int i = 0; i < 8; i++)
482 | {
483 | for (int j = 0; j < 8; j++)
484 | {
485 | if (m_field[i][j] != nullptr)
486 | {
487 | m_field[i][j]->render();
488 | }
489 | }
490 | }
491 | }
492 | }
493 |
494 | void Game::calcAllMoves()
495 | {
496 | for (int i = 0; i < 8; i++)
497 | {
498 | for (int j = 0; j < 8; j++)
499 | {
500 | if (m_field[i][j] != nullptr)
501 | {
502 | m_field[i][j]->calcPossibleMoves(m_field, true);
503 | }
504 | }
505 | }
506 | }
507 |
508 | bool Game::isValidMove(int x, int y, Piece* piece)
509 | {
510 | std::vector> list = piece->getPossibleMoves();
511 | for (const auto& value : list) {
512 | if (std::get<0>(value) == x && std::get<1>(value) == y)
513 | {
514 | return true;
515 | }
516 | }
517 | return false;
518 | }
519 |
--------------------------------------------------------------------------------
/src/Game.h:
--------------------------------------------------------------------------------
1 | #pragma once
2 | #include "Piece.h"
3 | #include "SDL_Handler.h"
4 | #include "Piece.h"
5 | #include "Pawn.h"
6 | #include "Rook.h"
7 | #include "Knight.h"
8 | #include "Bishop.h"
9 | #include "King.h"
10 | #include "Queen.h"
11 |
12 | class Game
13 | {
14 | public:
15 | // Constructor
16 | Game(SDL_Handler* handler);
17 |
18 | // Destructor
19 | ~Game();
20 |
21 | // returns a the Piece in field (row, col)
22 | Piece* getFieldPos(int row, int col);
23 |
24 | //Moves a piece
25 | void move(Piece* piece, std::tuple move);
26 |
27 | // returns m_turn
28 | Piece::Team getTurn() { return m_turn; };
29 |
30 | // true, if the move is valid ; false if not
31 | bool isValidMove(int x, int y, Piece* piece);
32 |
33 | // calculates all allowed moves of every piece
34 | void calcAllMoves();
35 |
36 | // light up the possible Moves
37 | void renderPossibleMoves(Piece* piece);
38 |
39 | // undos the renderPossibleMoves function
40 | void undoRenderPossibleMoves(Piece* piece);
41 |
42 | private:
43 | // 2D Field array, every Position has got a PIece::Team and a piece
44 | Piece* m_field[8][8];
45 |
46 | // disables enPassant for every Piece
47 | void disableEnPassant();
48 |
49 | // normal move
50 | void normal(int xStart, int yStart, int xEnd, int yEnd);
51 |
52 | // enpassant move
53 | void enPassant(int xStart, int yStart, int xEnd, int yEnd);
54 |
55 | // exchange move
56 | void exchange(int xStart, int yStart, int xEnd, int yEnd);
57 |
58 | // castles move
59 | void castles(int xStart, int yStart, int xEnd, int yEnd);
60 |
61 | // Background filename
62 | std::string m_backgroundFilename;
63 |
64 | // Decides whether if its black or whites turn
65 | Piece::Team m_turn;
66 |
67 | // handler
68 | SDL_Handler* m_handler;
69 |
70 | // if true, disable en Passant! if false, dont
71 | bool m_checkEnPassant;
72 |
73 | // checks current game state, determines winner or remis
74 | void gameState();
75 |
76 | // every single piece
77 | Pawn* pl1;
78 | Pawn* pl2;
79 | Pawn* pl3;
80 | Pawn* pl4;
81 | Pawn* pl5;
82 | Pawn* pl6;
83 | Pawn* pl7;
84 | Pawn* pl8;
85 | Pawn* pb1;
86 | Pawn* pb2;
87 | Pawn* pb3;
88 | Pawn* pb4;
89 | Pawn* pb5;
90 | Pawn* pb6;
91 | Pawn* pb7;
92 | Pawn* pb8;
93 | Rook* rb1;
94 | Rook* rb2;
95 | Rook* rl1;
96 | Rook* rl2;
97 | Knight* nb1;
98 | Knight* nb2;
99 | Knight* nl1;
100 | Knight* nl2;
101 | Bishop* bb1;
102 | Bishop* bb2;
103 | Bishop* bl1;
104 | Bishop* bl2;
105 | King* kb1;
106 | King* kl1;
107 | Queen* qb1;
108 | Queen* ql1;
109 | };
110 |
111 |
--------------------------------------------------------------------------------
/src/King.cpp:
--------------------------------------------------------------------------------
1 | #include "King.h"
2 | #include "Pawn.h"
3 | #include
4 | #include
5 |
6 | King::King(Team team, std::pair pos, SDL_Handler* handler)
7 | :Piece(team, pos, handler, KING), m_check(false)
8 | {
9 | std::string filename;
10 | if (team == BLACK)
11 | {
12 | filename = "../res/Chess_kdt60.png";
13 | }
14 | else
15 | {
16 | filename = "../res/Chess_klt60.png";
17 | }
18 | m_handler = handler;
19 | m_texture = handler->loadImage(filename);
20 |
21 | render();
22 | }
23 |
24 | void King::sayMyName()
25 | {
26 | if (m_team == BLACK)
27 | {
28 | std::cout << "BLACK KING" << std::endl;
29 | }
30 | else
31 | {
32 | std::cout << "WHTIE KING" << std::endl;
33 | }
34 | }
35 |
36 | void King::calcPossibleMoves(Piece* field[8][8], bool checkCheck)
37 | {
38 | std::vector> moves;
39 | bool castles = true;
40 | bool enemyPlace = false;
41 |
42 | for (int dx = -1; dx <= 1; dx++)
43 | {
44 | for (int dy = -1; dy <= 1; dy++)
45 | {
46 | if (m_pos.first + dx >= 0 && m_pos.first + dx <= 7 && m_pos.second + dy >= 0 && m_pos.second + dy <= 7)
47 | {
48 | if (field[m_pos.first + dx][m_pos.second + dy] != nullptr)
49 | {
50 | if (field[m_pos.first + dx][m_pos.second + dy]->getTeam() != m_team)
51 | {
52 | moves = pushMove(moves,
53 | std::tuple(m_pos.first + dx, m_pos.second + dy, Piece::NORMAL),
54 | getOwnKing(field),
55 | field,
56 | checkCheck);
57 | }
58 | }
59 | else
60 | {
61 | moves = pushMove(moves,
62 | std::tuple(m_pos.first + dx, m_pos.second + dy, Piece::NORMAL),
63 | getOwnKing(field),
64 | field,
65 | checkCheck);
66 | }
67 | }
68 | }
69 | }
70 |
71 | if (!m_hasMoved)
72 | {
73 | for (int i = 0; i <= 7; i += 7)
74 | {
75 | for (int j = 0; j <= 7; j += 7)
76 | {
77 | castles = true;
78 | if (field[i][j] != nullptr)
79 | {
80 | if (field[i][j]->getTeam() == m_team && field[i][j]->getType() == ROOK && !field[i][j]->m_hasMoved)
81 | {
82 | int a, b, c;
83 | if (i == 0)
84 | {
85 | a = 1;
86 | b = 2;
87 | c = 3;
88 | }
89 | else
90 | {
91 | a = 5;
92 | b = 6;
93 | c = 6;
94 | }
95 | if (field[a][j] == nullptr && field[b][j] == nullptr && field[c][j] == nullptr)
96 | {
97 | for (int k = 0; k < 8; k++)
98 | {
99 | for (int l = 0; l < 8; l++)
100 | {
101 | if (field[k][l] != nullptr)
102 | {
103 | if (field[k][l]->getTeam() != m_team)
104 | {
105 | std::vector> notPossible = field[k][l]->getPossibleMoves();
106 | for (const auto& value : notPossible)
107 | {
108 | if (i == 0)
109 | {
110 | if ((std::get<0>(value) == 4 && std::get<1>(value) == j)
111 | || (std::get<0>(value) == 2 && std::get<1>(value) == j)
112 | || (std::get<0>(value) == 3 && std::get<1>(value) == j))
113 | {
114 | castles = false;
115 | }
116 | }
117 | else
118 | {
119 | if ((std::get<0>(value) == 5 && std::get<1>(value) == j)
120 | || (std::get<0>(value) == 6 && std::get<1>(value) == j)
121 | || (std::get<0>(value) == 4 && std::get<1>(value) == j))
122 | {
123 | castles = false;
124 | }
125 | }
126 | }
127 | }
128 | }
129 | }
130 | }
131 | if (castles)
132 | {
133 | moves.push_back(std::tuple(i, j, Piece::CASTLE));
134 | }
135 | }
136 | }
137 | }
138 | }
139 | }
140 | }
141 | m_possibleMoves = moves;
142 | }
143 |
144 | void King::setCheck(Piece* field[8][8], int x, int y)
145 | {
146 | bool check = false;
147 |
148 | for (int i = 0; i < 8; i++)
149 | {
150 | for (int j = 0; j < 8; j++)
151 | {
152 | if (field[i][j] != nullptr)
153 | {
154 | if (field[i][j]->getTeam() != m_team)
155 | {
156 | if (field[i][j]->getType() == KING)
157 | {
158 | if (abs(field[i][j]->getPos().first - x) <= 1 && abs(field[i][j]->getPos().second - y) <= 1)
159 | {
160 | check = true;
161 | }
162 |
163 | }
164 | else if (field[i][j]->getType() == PAWN)
165 | {
166 | int dy_pawn;
167 | if (field[i][j]->getTeam() == WHITE)
168 | {
169 | dy_pawn = 1;
170 | }
171 | else
172 | {
173 | dy_pawn = -1;
174 | }
175 | if ((x == field[i][j]->getPos().first + 1 || x == field[i][j]->getPos().first - 1) && y == field[i][j]->getPos().second + dy_pawn)
176 | {
177 | check = true;
178 | }
179 | }
180 | else
181 | {
182 | field[i][j]->calcPossibleMoves(field, false);
183 | std::vector> notPossible = field[i][j]->getPossibleMoves();
184 | for (const auto& value : notPossible)
185 | {
186 | if (std::get<0>(value) == x && std::get<1>(value) == y)
187 | {
188 | check = true;
189 | }
190 | }
191 | }
192 | }
193 | }
194 | }
195 | }
196 |
197 | m_check = check;
198 | }
199 |
--------------------------------------------------------------------------------
/src/King.h:
--------------------------------------------------------------------------------
1 | #pragma once
2 | #include "Piece.h"
3 |
4 | class King :
5 | public Piece
6 | {
7 | public:
8 | King(Team team, std::pair pos, SDL_Handler* handler);
9 |
10 | // calls type and color to debug
11 | void sayMyName();
12 |
13 | // calculates the possible moves
14 | void calcPossibleMoves(Piece* field[8][8], bool checkCheck);
15 |
16 | // controls whether this piece is checked or not
17 | void setCheck(Piece* field[8][8], int x, int y);
18 |
19 | // get check
20 | bool getCheck() { return m_check; };
21 |
22 | private:
23 |
24 | // true, if the king is on any of the other teams movelist's
25 | bool m_check;
26 | };
27 |
28 |
--------------------------------------------------------------------------------
/src/Knight.cpp:
--------------------------------------------------------------------------------
1 | #include "Knight.h"
2 | #include
3 |
4 | Knight::Knight(Team team, std::pair pos, SDL_Handler* handler)
5 | :Piece(team, pos, handler, KNIGHT)
6 | {
7 | std::string filename;
8 | if (team == BLACK)
9 | {
10 | filename = "../res/Chess_ndt60.png";
11 | }
12 | else
13 | {
14 | filename = "../res/Chess_nlt60.png";
15 | }
16 | m_handler = handler;
17 | m_texture = handler->loadImage(filename);
18 | render();
19 | }
20 |
21 | void Knight::sayMyName()
22 | {
23 | if (m_team == BLACK)
24 | {
25 | std::cout << "BLACK KNIGHT" << std::endl;
26 | }
27 | else
28 | {
29 | std::cout << "WHTIE KNIGHT" << std::endl;
30 | }
31 | }
32 |
33 | void Knight::calcPossibleMoves(Piece* field[8][8], bool checkCheck)
34 | {
35 | std::vector> moves;
36 |
37 | for (int dx = -2; dx <= 2 ; dx += 4)
38 | {
39 | for (int dy = -1; dy <= 1; dy += 2)
40 | {
41 | if (m_pos.first + dx >= 0 && m_pos.first + dx <= 7 && m_pos.second + dy >= 0 && m_pos.second + dy <= 7)
42 | {
43 | if (field[m_pos.first + dx][m_pos.second + dy] == nullptr)
44 | {
45 | moves = pushMove(moves,
46 | std::tuple(m_pos.first + dx, m_pos.second + dy, Piece::NORMAL),
47 | getOwnKing(field),
48 | field,
49 | checkCheck);
50 | }
51 | else if (field[m_pos.first + dx][m_pos.second + dy] != nullptr)
52 | {
53 | if (field[m_pos.first + dx][m_pos.second + dy]->getTeam() != m_team)
54 | {
55 | moves = pushMove(moves,
56 | std::tuple(m_pos.first + dx, m_pos.second + dy, Piece::NORMAL),
57 | getOwnKing(field),
58 | field,
59 | checkCheck);
60 | }
61 | }
62 | }
63 | }
64 | }
65 |
66 | for (int dy = -2; dy <= 2; dy += 4)
67 | {
68 | for (int dx = -1; dx <= 1; dx += 2)
69 | {
70 | if (m_pos.first + dx >= 0 && m_pos.first + dx <= 7 && m_pos.second + dy >= 0 && m_pos.second + dy <= 7)
71 | {
72 | if (field[m_pos.first + dx][m_pos.second + dy] == nullptr)
73 | {
74 | moves = pushMove(moves,
75 | std::tuple(m_pos.first + dx, m_pos.second + dy, Piece::NORMAL),
76 | getOwnKing(field),
77 | field,
78 | checkCheck);
79 | }
80 | else if (field[m_pos.first + dx][m_pos.second + dy] != nullptr)
81 | {
82 | if (field[m_pos.first + dx][m_pos.second + dy]->getTeam() != m_team)
83 | {
84 | moves = pushMove(moves,
85 | std::tuple(m_pos.first + dx, m_pos.second + dy, Piece::NORMAL),
86 | getOwnKing(field),
87 | field,
88 | checkCheck);
89 | }
90 | }
91 | }
92 | }
93 | }
94 |
95 | m_possibleMoves = moves;
96 | }
--------------------------------------------------------------------------------
/src/Knight.h:
--------------------------------------------------------------------------------
1 | #pragma once
2 | #include "Piece.h"
3 |
4 | class Knight :
5 | public Piece
6 | {
7 | public:
8 | Knight(Team team, std::pair pos, SDL_Handler* handler);
9 |
10 | // calls name an
11 | void sayMyName();
12 |
13 | // calculates the possible moves
14 | void calcPossibleMoves(Piece* field[8][8], bool checkCheck);
15 | };
16 |
17 |
--------------------------------------------------------------------------------
/src/Main.cpp:
--------------------------------------------------------------------------------
1 | #include "MainLoop.h"
2 |
3 | int main(int argc, char* args[])
4 | {
5 | MainLoop::run();
6 | return 0;
7 | }
8 |
--------------------------------------------------------------------------------
/src/MainLoop.cpp:
--------------------------------------------------------------------------------
1 | #include
2 | #include
3 | #include "MainLoop.h"
4 | #include "SDL_Handler.h"
5 | #include "Game.h"
6 |
7 | void MainLoop::run()
8 | {
9 | SDL_Handler* handler = new SDL_Handler();
10 | handler->init();
11 |
12 | handler->renderBackground();
13 |
14 | Game* game = new Game(handler);
15 | bool quit = false;
16 |
17 | int xStart = -1;
18 | int yStart = -1;
19 | int xEnd = -1;
20 | int yEnd = -1;
21 | Piece* clickedOn = nullptr;
22 |
23 | while (quit == false)
24 | {
25 | while (SDL_PollEvent(&handler->m_event))
26 | {
27 | if (handler->m_event.type == SDL_QUIT)
28 | {
29 | quit = true;
30 | }
31 |
32 | if (handler->m_event.type == SDL_MOUSEBUTTONDOWN)
33 | {
34 | xStart = handler->m_event.button.x / 80;
35 | yStart = handler->m_event.button.y / 80;
36 | clickedOn = game->getFieldPos(xStart, yStart);
37 | if (clickedOn != nullptr)
38 | {
39 | if (clickedOn->getTeam() == game->getTurn())
40 | {
41 | game->renderPossibleMoves(clickedOn);
42 | }
43 | }
44 | }
45 |
46 | if (handler->m_event.type == SDL_MOUSEBUTTONUP)
47 | {
48 | if (clickedOn != nullptr)
49 | {
50 | if (clickedOn->getTeam() == game->getTurn())
51 | {
52 | game->undoRenderPossibleMoves(clickedOn);
53 | }
54 | }
55 | xEnd = handler->m_event.button.x / 80;
56 | yEnd = handler->m_event.button.y / 80;
57 | if (clickedOn != nullptr)
58 | {
59 | if ((xStart != -1 && yStart != -1 && xEnd != -1 && yEnd != -1)
60 | && (clickedOn->getTeam() == game->getTurn())
61 | && (game->isValidMove(xEnd, yEnd, clickedOn)))
62 | {
63 | std::vector> list = game->getFieldPos(xStart, yStart)->getPossibleMoves();
64 | for (const auto& value : list)
65 | {
66 | if (std::get<0>(value) == xEnd && std::get<1>(value) == yEnd)
67 | {
68 | game->move(clickedOn, std::tuple(xEnd, yEnd, std::get<2>(value)));
69 | }
70 | }
71 | xStart = -1;
72 | yStart = -1;
73 | yEnd = -1;
74 | game->calcAllMoves();
75 | clickedOn = nullptr;
76 | }
77 | }
78 | }
79 | }
80 | }
81 |
82 | handler->cleanUp();
83 | }
--------------------------------------------------------------------------------
/src/MainLoop.h:
--------------------------------------------------------------------------------
1 | #pragma once
2 | class MainLoop
3 | {
4 | public:
5 | static void run();
6 | };
7 |
8 |
--------------------------------------------------------------------------------
/src/Pawn.cpp:
--------------------------------------------------------------------------------
1 | #include "Pawn.h"
2 | #include
3 | #include
4 |
5 | Pawn::Pawn(Team team, std::pair pos, SDL_Handler* handler)
6 | :Piece(team, pos, handler, PAWN), m_enPassant(std::pair(false, 0))
7 | {
8 | std::string filename;
9 | if (team == BLACK)
10 | {
11 | filename = "../res/Chess_pdt60.png";
12 | }
13 | else
14 | {
15 | filename = "../res/Chess_plt60.png";
16 | }
17 | m_handler = handler;
18 | m_texture = handler->loadImage(filename);
19 |
20 | if (team == BLACK)
21 | {
22 | m_dy = -1;
23 | }
24 | else
25 | {
26 | m_dy = 1;
27 | }
28 |
29 | render();
30 | }
31 |
32 | void Pawn::sayMyName()
33 | {
34 | if (m_team == BLACK)
35 | {
36 | std::cout << "BLACK PAWN" << std::endl;
37 | }
38 | else
39 | {
40 | std::cout << "WHITE PAWN" << std::endl;
41 | }
42 | }
43 |
44 | void Pawn::calcPossibleMoves(Piece* field[8][8], bool checkCheck)
45 | {
46 | std::vector> moves;
47 |
48 | if (m_pos.second + m_dy == 0 || m_pos.second + m_dy == 7)
49 | {
50 | if (field[m_pos.first][m_pos.second + m_dy] == nullptr)
51 | {
52 | moves = pushMove(moves,
53 | std::tuple(m_pos.first, m_pos.second + m_dy, Piece::NEWPIECE),
54 | getOwnKing(field),
55 | field,
56 | checkCheck);
57 | }
58 | }
59 | else
60 | {
61 | if (field[m_pos.first][m_pos.second + m_dy] == nullptr)
62 | {
63 | moves = pushMove(moves,
64 | std::tuple(m_pos.first, m_pos.second + m_dy, Piece::NORMAL),
65 | getOwnKing(field),
66 | field,
67 | checkCheck);
68 | }
69 | }
70 |
71 | if ((m_pos.second + 2 * m_dy >= 0) && (m_pos.second + 2 * m_dy <= 7))
72 | {
73 | if (field[m_pos.first][m_pos.second + 2 * m_dy] == nullptr && !m_hasMoved)
74 | {
75 | moves = pushMove(moves,
76 | std::tuple(m_pos.first, m_pos.second + 2 * m_dy, Piece::NORMAL),
77 | getOwnKing(field),
78 | field,
79 | checkCheck);
80 | }
81 | }
82 |
83 | if (m_pos.first + 1 <= 7)
84 | {
85 | if (field[m_pos.first + 1][m_pos.second + m_dy] != nullptr)
86 | {
87 | if (field[m_pos.first + 1][m_pos.second + m_dy]->getTeam() != m_team)
88 | {
89 | if (m_pos.second + m_dy == 0 || m_pos.second + m_dy == 7)
90 | {
91 | moves = pushMove(moves,
92 | std::tuple(m_pos.first + 1, m_pos.second + m_dy, Piece::NEWPIECE),
93 | getOwnKing(field),
94 | field,
95 | checkCheck);
96 | }
97 | else
98 | {
99 | moves = pushMove(moves,
100 | std::tuple(m_pos.first + 1, m_pos.second + m_dy, Piece::NORMAL),
101 | getOwnKing(field),
102 | field,
103 | checkCheck);
104 | }
105 | }
106 | }
107 | }
108 | if (m_pos.first - 1 >= 0)
109 | {
110 | if (field[m_pos.first - 1][m_pos.second + m_dy] != nullptr)
111 | {
112 | if (field[m_pos.first - 1][m_pos.second + m_dy]->getTeam() != m_team)
113 | {
114 | if (m_pos.second + m_dy == 0 || m_pos.second + m_dy == 7)
115 | {
116 | moves = pushMove(moves,
117 | std::tuple(m_pos.first - 1, m_pos.second + m_dy, Piece::NEWPIECE),
118 | getOwnKing(field),
119 | field,
120 | checkCheck);
121 | }
122 | else
123 | {
124 | moves = pushMove(moves,
125 | std::tuple(m_pos.first - 1, m_pos.second + m_dy, Piece::NORMAL),
126 | getOwnKing(field),
127 | field,
128 | checkCheck);
129 | }
130 | }
131 | }
132 | }
133 |
134 | if (m_enPassant == std::pair(true, -1))
135 | {
136 | moves = pushMove(moves,
137 | std::tuple(m_pos.first + 1, m_pos.second + m_dy, Piece::ENPASSANT),
138 | getOwnKing(field),
139 | field,
140 | checkCheck);
141 | }
142 | if (m_enPassant == std::pair(true, 1))
143 | {
144 | moves = pushMove(moves,
145 | std::tuple(m_pos.first - 1, m_pos.second + m_dy, Piece::ENPASSANT),
146 | getOwnKing(field),
147 | field,
148 | checkCheck);
149 | }
150 | m_possibleMoves = moves;
151 | }
152 |
153 |
154 |
155 |
156 |
157 |
158 |
--------------------------------------------------------------------------------
/src/Pawn.h:
--------------------------------------------------------------------------------
1 | #pragma once
2 | #include "Piece.h"
3 |
4 | class Pawn :
5 | public Piece
6 | {
7 | public:
8 | // constructor
9 | Pawn(Team team, std::pair pos, SDL_Handler* handler);
10 |
11 | // prints name of piece
12 | void sayMyName();
13 |
14 | // calculates possible moves
15 | void calcPossibleMoves(Piece* field[8][8], bool checkCheck);
16 |
17 | // direction the pawn moves
18 | int m_dy;
19 |
20 | // getter m_enPassant
21 | std::pair getEnPassant() { return m_enPassant; };
22 |
23 | // set m_enPassant
24 | void setEnPassant(std::pair modifi) { m_enPassant = modifi; };
25 |
26 | private:
27 | // if true, en passant is possible in the int direction
28 | std::pair m_enPassant;
29 | };
30 |
31 |
--------------------------------------------------------------------------------
/src/Piece.cpp:
--------------------------------------------------------------------------------
1 | #include "Piece.h"
2 | #include "King.h"
3 | #include
4 | #include
5 |
6 | std::vector> Piece::pushMove(std::vector> moveList,
7 | std::tuple move,
8 | King* king,
9 | Piece* field[8][8],
10 | bool checkCheck)
11 | {
12 | if (!checkCheck)
13 | {
14 | moveList.push_back(move);
15 | }
16 | else
17 | {
18 | bool enemyPlace = true;
19 | king->setCheck(field, king->getPos().first, king->getPos().second);
20 | Piece* zwisch = &(*field[std::get<0>(move)][std::get<1>(move)]);
21 | enemyPlace = false;
22 |
23 | if (field[std::get<0>(move)][std::get<1>(move)] != nullptr)
24 | {
25 | enemyPlace = true;
26 | field[std::get<0>(move)][std::get<1>(move)] = nullptr;
27 | }
28 |
29 | std::swap(field[std::get<0>(move)][std::get<1>(move)], field[m_pos.first][m_pos.second]);
30 | if (m_type == KING)
31 | {
32 | king->setCheck(field, std::get<0>(move), std::get<1>(move));
33 | }
34 | else
35 | {
36 | king->setCheck(field, king->getPos().first, king->getPos().second);
37 | }
38 | std::swap(field[std::get<0>(move)][std::get<1>(move)], field[m_pos.first][m_pos.second]);
39 |
40 | if (enemyPlace)
41 | {
42 | field[std::get<0>(move)][std::get<1>(move)] = &(*zwisch);
43 | }
44 | if (!king->getCheck())
45 | {
46 | moveList.push_back(move);
47 | }
48 | king->setCheck(field, king->getPos().first, king->getPos().second);
49 | }
50 | return moveList;
51 | }
52 |
53 | King* Piece::getOwnKing(Piece* field[8][8])
54 | {
55 | for (int i = 0; i < 8; i++)
56 | {
57 | for (int j = 0; j < 8; j++)
58 | {
59 | if (field[i][j] != nullptr)
60 | {
61 | if (field[i][j]->getTeam() == m_team && field[i][j]->getType() == Piece::KING)
62 | {
63 | King* ret = static_cast(field[i][j]);
64 | return ret;
65 | }
66 | }
67 | }
68 | }
69 | return nullptr;
70 | }
71 |
72 | Piece::Piece(Team team, std::pair pos, SDL_Handler* handler, PieceType type)
73 | :m_team(team), m_pos(pos), m_handler(handler), m_texture(NULL), m_hasMoved(false), m_type(type)
74 | {
75 | }
76 |
77 | Piece::Piece(const Piece& piece)
78 | :m_team(piece.m_team), m_pos(piece.m_pos), m_handler(piece.m_handler), m_texture(NULL), m_hasMoved(false), m_type(piece.m_type)
79 | {
80 | }
81 |
82 | Piece::~Piece()
83 | {
84 | SDL_DestroyTexture(m_texture);
85 |
86 | m_handler->undoPieceRender(m_pos.first, m_pos.second);
87 | }
88 |
89 |
90 | void Piece::render()
91 | {
92 | SDL_Rect src = {0, 0, 60, 60};
93 | if (m_handler == nullptr)
94 | {
95 | sayMyName();
96 | }
97 | SDL_Rect dest = { m_handler->SCREEN_WIDTH / 8 * m_pos.first - 2,
98 | m_handler->SCREEN_HEIGHT / 8 * m_pos.second,
99 | m_handler->SCREEN_WIDTH / 8,
100 | m_handler->SCREEN_HEIGHT / 8 };
101 | m_handler->DrawRectangle(src, dest, m_texture);
102 | }
103 |
104 | void Piece::sayMyName()
105 | {
106 | std::cout << "No Piece here" << std::endl;
107 | }
108 |
--------------------------------------------------------------------------------
/src/Piece.h:
--------------------------------------------------------------------------------
1 | #pragma once
2 |
3 | #include
4 | #include
5 | #include "SDL_Handler.h"
6 | #include
7 | #include
8 |
9 | class King;
10 |
11 | class Piece
12 | {
13 | public:
14 |
15 | enum Team { BLACK, WHITE, NONE };
16 |
17 | enum PieceType { PAWN, ROOK, KNIGHT, BISHOP, KING, QUEEN, EMPTY };
18 |
19 | enum MoveType { NORMAL, CASTLE, ENPASSANT, NEWPIECE, INIT };
20 |
21 | // returns list of possible Moves
22 | std::vector> getPossibleMoves() { return m_possibleMoves; };
23 |
24 | // return whether BLACK or WHITE
25 | Team getTeam() { return m_team; };
26 |
27 | // sets new position
28 | void setPosition(std::pair newPos) { m_pos = newPos; };
29 |
30 | // return position of piece
31 | std::pair getPos() { return m_pos; };
32 |
33 | // Constructor
34 | Piece(Team team, std::pair pos, SDL_Handler* handler, PieceType type);
35 |
36 | // Copy-Constructor
37 | Piece(const Piece& piece);
38 |
39 | // Destructor
40 | ~Piece();
41 |
42 | //render this piece
43 | void render();
44 |
45 | // prints name of piece
46 | virtual void sayMyName();
47 |
48 | // calculates every possible Move this piece can do
49 | virtual void calcPossibleMoves(Piece* field[8][8], bool checkCheck) = 0;
50 |
51 | // true, if piece has moved
52 | bool m_hasMoved;
53 |
54 | // returns type of piece
55 | PieceType getType() { return m_type; };
56 |
57 | protected:
58 |
59 | // texture of this piece
60 | SDL_Texture* m_texture;
61 |
62 | // SDL Handler
63 | SDL_Handler* m_handler;
64 |
65 | // Team this piece plays for
66 | Team m_team;
67 |
68 | // The Type of Piece
69 | PieceType m_type;
70 |
71 | // List of possible Moves this piece can do
72 | std::vector> m_possibleMoves;
73 |
74 | // Position of the piece
75 | std::pair m_pos;
76 |
77 | // pushes the move, if its allowed.
78 | // simulates the move, and checks wheter the own king is still checked
79 | // if king is in check after simulated move, the move is not allowed
80 | // if checkCheck is true the king simulation will determine whether the move is allowed or not
81 | // if checkCheck is false, we will just push the move. checkCheck is only false in King's setCheck function,
82 | // because otherwise it will produce stack overflow (pushMove calls setCheck, setCheck calls pushMove and so on)
83 | std::vector> pushMove(std::vector> moveList,
84 | std::tuple move,
85 | King* king,
86 | Piece* field[8][8],
87 | bool checkCheck);
88 |
89 | // returns king of own team from field
90 | King* getOwnKing(Piece* field[8][8]);
91 | };
92 |
93 |
--------------------------------------------------------------------------------
/src/Queen.cpp:
--------------------------------------------------------------------------------
1 | #include "Queen.h"
2 | #include
3 |
4 | Queen::Queen(Team team, std::pair pos, SDL_Handler* handler)
5 | :Piece(team, pos, handler, QUEEN)
6 | {
7 | std::string filename;
8 | if (team == BLACK)
9 | {
10 | filename = "../res/Chess_qdt60.png";
11 | }
12 | else
13 | {
14 | filename = "../res/Chess_qlt60.png";
15 | }
16 | m_handler = handler;
17 | m_texture = handler->loadImage(filename);
18 | render();
19 | }
20 |
21 |
22 | void Queen::sayMyName()
23 | {
24 | if (m_team == BLACK)
25 | {
26 | std::cout << "BLACK QUEEN" << std::endl;
27 | }
28 | else
29 | {
30 | std::cout << "WHTIE QUEEN" << std::endl;
31 | }
32 | }
33 |
34 | void Queen::calcPossibleMoves(Piece* field[8][8], bool checkCheck)
35 | {
36 | std::vector> moves;
37 | int dx_copy;
38 | int dy_copy;
39 | for (int dx = -1; dx <= 1; dx++)
40 | {
41 | for (int dy = -1; dy <= 1; dy++)
42 | {
43 | dx_copy = dx;
44 | dy_copy = dy;
45 | while (field[m_pos.first + dx_copy][m_pos.second + dy_copy] == nullptr)
46 | {
47 | moves = pushMove(moves,
48 | std::tuple (m_pos.first + dx_copy, m_pos.second + dy_copy, Piece::NORMAL),
49 | getOwnKing(field),
50 | field,
51 | checkCheck);
52 | if (dx_copy < 0)
53 | {
54 | dx_copy -= 1;
55 | }
56 | else if (dx_copy > 0)
57 | {
58 | dx_copy += 1;
59 | }
60 | if (dy_copy < 0)
61 | {
62 | dy_copy -= 1;
63 | }
64 | else if (dy_copy > 0)
65 | {
66 | dy_copy += 1;
67 | }
68 | }
69 | if (field[m_pos.first + dx_copy][m_pos.second + dy_copy] != nullptr
70 | && (m_pos.first + dx_copy >= 0 && m_pos.first + dx_copy <= 7 && m_pos.second + dy_copy >= 0 && m_pos.second + dy_copy <= 7))
71 | {
72 | if (field[m_pos.first + dx_copy][m_pos.second + dy_copy]->getTeam() != m_team)
73 | {
74 | moves = pushMove(moves,
75 | std::tuple (m_pos.first + dx_copy, m_pos.second + dy_copy, Piece::NORMAL),
76 | getOwnKing(field),
77 | field,
78 | checkCheck);
79 | }
80 | }
81 | }
82 | }
83 |
84 | m_possibleMoves = moves;
85 | }
--------------------------------------------------------------------------------
/src/Queen.h:
--------------------------------------------------------------------------------
1 | #pragma once
2 | #include "Piece.h"
3 | class Queen :
4 | public Piece
5 | {
6 | public:
7 | Queen(Team team, std::pair pos, SDL_Handler* handler);
8 |
9 | // prints name of piece
10 | void sayMyName();
11 |
12 | // calculates the possible moves
13 | void calcPossibleMoves(Piece* field[8][8], bool checkCheck);
14 | };
15 |
16 |
--------------------------------------------------------------------------------
/src/Rook.cpp:
--------------------------------------------------------------------------------
1 | #include "Rook.h"
2 | #include
3 |
4 | Rook::Rook(Team team, std::pair pos, SDL_Handler* handler)
5 | :Piece(team, pos, handler, ROOK)
6 | {
7 | std::string filename;
8 | if (team == BLACK)
9 | {
10 | filename = "../res/Chess_rdt60.png";
11 | }
12 | else
13 | {
14 | filename = "../res/Chess_rlt60.png";
15 | }
16 | m_handler = handler;
17 | m_texture = handler->loadImage(filename);
18 |
19 | render();
20 | }
21 |
22 | void Rook::sayMyName()
23 | {
24 | if (m_team == BLACK)
25 | {
26 | std::cout << "BLACK ROOK" << std::endl;
27 | }
28 | else
29 | {
30 | std::cout << "WHITE ROOK" << std::endl;
31 | }
32 | }
33 |
34 | void Rook::calcPossibleMoves(Piece* field[8][8], bool checkCheck)
35 | {
36 | std::vector> moves;
37 |
38 | int dx_copy;
39 | int dy_copy;
40 | for (int dx = -1; dx <= 1; dx ++)
41 | {
42 | for (int dy = -1; dy <= 1; dy ++)
43 | {
44 | if (dy == 0 || dx == 0)
45 | {
46 | dx_copy = dx;
47 | dy_copy = dy;
48 | while (field[m_pos.first + dx_copy][m_pos.second + dy_copy] == nullptr
49 | && (m_pos.first + dx_copy >= 0 && m_pos.first + dx_copy <= 7 && m_pos.second + dy_copy >= 0 && m_pos.second + dy_copy <= 7))
50 | {
51 | moves = pushMove(moves,
52 | std::tuple(m_pos.first + dx_copy, m_pos.second + dy_copy, Piece::NORMAL),
53 | getOwnKing(field),
54 | field,
55 | checkCheck);
56 |
57 | if (dx_copy < 0)
58 | {
59 | dx_copy -= 1;
60 | }
61 | else if (dx_copy > 0)
62 | {
63 | dx_copy += 1;
64 | }
65 | if (dy_copy < 0)
66 | {
67 | dy_copy -= 1;
68 | }
69 | else if (dy_copy > 0)
70 | {
71 | dy_copy += 1;
72 | }
73 | }
74 | if (field[m_pos.first + dx_copy][m_pos.second + dy_copy] != nullptr
75 | && (m_pos.first + dx_copy >= 0 && m_pos.first + dx_copy <= 7 && m_pos.second + dy_copy >= 0 && m_pos.second + dy_copy <= 7))
76 | {
77 | if (field[m_pos.first + dx_copy][m_pos.second + dy_copy]->getTeam() != m_team)
78 | {
79 | moves = pushMove(moves,
80 | std::tuple(m_pos.first + dx_copy, m_pos.second + dy_copy, Piece::NORMAL),
81 | getOwnKing(field),
82 | field,
83 | checkCheck);
84 | }
85 | }
86 | }
87 | }
88 | }
89 |
90 | m_possibleMoves = moves;
91 | }
92 |
93 |
94 | Rook::~Rook()
95 | {
96 | }
97 |
--------------------------------------------------------------------------------
/src/Rook.h:
--------------------------------------------------------------------------------
1 | #pragma once
2 | #include "Piece.h"
3 |
4 | class Rook :
5 | public Piece
6 | {
7 | public:
8 | // Constructor
9 | Rook(Team team, std::pair pos, SDL_Handler* handler);
10 |
11 | // prints name of piece
12 | void sayMyName();
13 |
14 | // Destructor
15 | ~Rook();
16 |
17 | // calculates the possible moves
18 | void calcPossibleMoves(Piece* field[8][8], bool checkCheck);
19 | };
20 |
21 |
22 |
23 |
--------------------------------------------------------------------------------
/src/SDL2.dll:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/JuUnland/Chess/77c4951e370133d3342c731d3f6cf72312c4f684/src/SDL2.dll
--------------------------------------------------------------------------------
/src/SDL_Handler.cpp:
--------------------------------------------------------------------------------
1 | #include "SDL_Handler.h"
2 | #include
3 | #include
4 |
5 |
6 | SDL_Texture* SDL_Handler::loadImage(std::string filename)
7 | {
8 | SDL_Surface* loadedImage = NULL;
9 |
10 | loadedImage = IMG_Load(filename.c_str());
11 |
12 | if (loadedImage == NULL)
13 | {
14 | std::cout << "couldnt load " << filename << std::endl;
15 | }
16 |
17 | SDL_Texture* text = SDL_CreateTextureFromSurface(m_renderer, loadedImage);
18 |
19 | return text;
20 | }
21 |
22 | void SDL_Handler::renderBackground()
23 | {
24 | bool white = true;
25 | SDL_SetRenderDrawColor(m_renderer, 255, 255, 255, 255);
26 |
27 | for (int i = 0; i < 8; i++)
28 | {
29 | for (int j = 0; j < 8; j++)
30 | {
31 | if (white)
32 | {
33 | SDL_SetRenderDrawColor(m_renderer, 255, 255, 255, 255);
34 | }
35 | else
36 | {
37 | SDL_SetRenderDrawColor(m_renderer, 155, 103, 60, 255);
38 | }
39 | white = !white;
40 | SDL_Rect rectangle = { i * SCREEN_WIDTH / 8,
41 | j * SCREEN_HEIGHT / 8,
42 | SCREEN_WIDTH / 8,
43 | SCREEN_HEIGHT / 8 };
44 | SDL_RenderFillRect(m_renderer, &rectangle);
45 | }
46 | white = !white;
47 | }
48 | }
49 |
50 | void SDL_Handler::undoPieceRender(int x, int y)
51 | {
52 | if ((x % 2 == 0 && y % 2 == 0) || (x % 2 == 1 && y % 2 == 1))
53 | {
54 | SDL_SetRenderDrawColor(m_renderer, 255, 255, 255, 255);
55 | }
56 | else
57 | {
58 | SDL_SetRenderDrawColor(m_renderer, 155, 103, 60, 255);
59 | }
60 | SDL_Rect rectangle = { x * SCREEN_WIDTH / 8,
61 | y * SCREEN_HEIGHT / 8,
62 | SCREEN_WIDTH / 8,
63 | SCREEN_HEIGHT / 8 };
64 | SDL_RenderFillRect(m_renderer, &rectangle);
65 | }
66 |
67 | void SDL_Handler::cleanUp()
68 | {
69 | SDL_FreeSurface(m_screenSurface);
70 | SDL_DestroyWindow(m_window);
71 | SDL_DestroyRenderer(m_renderer);
72 | SDL_Quit();
73 | }
74 |
75 |
76 | void SDL_Handler::DrawRectangle(SDL_Rect source, SDL_Rect dest, SDL_Texture* text)
77 | {
78 | if (text != nullptr)
79 | {
80 | SDL_RenderCopy(m_renderer, text, &source, &dest);
81 | SDL_RenderPresent(m_renderer);
82 |
83 | SDL_UpdateWindowSurface(m_window);
84 | }
85 | else
86 | {
87 | std::cout << "DrawRectangle: text was nullptr" << std::endl;
88 | }
89 | }
90 |
91 |
92 |
93 | SDL_Handler::~SDL_Handler()
94 | {
95 | }
96 |
97 |
98 | bool SDL_Handler::init()
99 | {
100 | m_window = NULL;
101 | m_screenSurface = NULL;
102 |
103 | bool quit = false;
104 |
105 | //Initialize SDL
106 | if (SDL_Init(SDL_INIT_VIDEO) < 0)
107 | {
108 | printf("SDL could not initialize! SDL_Error: %s\n", SDL_GetError());
109 | cleanUp();
110 | return false;
111 | }
112 | else
113 | {
114 | //Create window
115 | m_window = SDL_CreateWindow("Chess", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN);
116 | if (m_window == NULL)
117 | {
118 | printf("Window could not be created! SDL_Error: %s\n", SDL_GetError());
119 | }
120 | else
121 | {
122 | m_renderer = SDL_CreateRenderer(m_window, -1, 0);
123 | }
124 | }
125 | return true;
126 | }
127 |
--------------------------------------------------------------------------------
/src/SDL_Handler.h:
--------------------------------------------------------------------------------
1 | #pragma once
2 | #include
3 | #include
4 | #include
5 | #include
6 |
7 |
8 | class SDL_Handler
9 | {
10 | public:
11 |
12 | // Screen size
13 | const int SCREEN_WIDTH = 640;
14 | const int SCREEN_HEIGHT = 640;
15 |
16 | // window we'll be rendering to
17 | SDL_Window* m_window;
18 |
19 | // Surface contained by the window
20 | SDL_Surface* m_screenSurface;
21 |
22 | // Renderer
23 | SDL_Renderer* m_renderer;
24 |
25 | // Event (Mouseclick etc)
26 | SDL_Event m_event;
27 |
28 | // initialize the field
29 | // returns true, if everything worked
30 | bool init();
31 |
32 | // cleans up SDL stuff
33 | void cleanUp();
34 |
35 | // destructor
36 | ~SDL_Handler();
37 |
38 | // renders the source rectangle of the texture to dest rectangle
39 | void DrawRectangle(SDL_Rect source, SDL_Rect dest, SDL_Texture* text);
40 |
41 | // removes PieceRender
42 | void undoPieceRender(int x, int y);
43 |
44 | // gets filename, return texture of the jpg file
45 | SDL_Texture* loadImage(std::string filename);
46 |
47 | //renders the background of the field
48 | void renderBackground();
49 |
50 | private:
51 |
52 | };
53 |
54 |
--------------------------------------------------------------------------------