├── .github
└── FUNDING.yml
├── .gitignore
├── LICENSE
├── README.md
└── src
├── TicTacToe
├── App.cs
├── Local
│ ├── Converter
│ │ └── EqualsToVisibilityConverter.cs
│ ├── Data
│ │ ├── Player.cs
│ │ ├── PlayerItem.cs
│ │ └── SquareItem.cs
│ └── Mvvm
│ │ └── MainViewModel.cs
├── Properties
│ └── AssemblyInfo.cs
├── Starter.cs
├── Themes
│ ├── Generic.xaml
│ ├── Units
│ │ ├── GameBoard.xaml
│ │ ├── GameBoardItem.xaml
│ │ ├── PlayerCard.xaml
│ │ ├── ScoreBox.xaml
│ │ └── ScoreBoxItem.xaml
│ └── Views
│ │ └── MainWindow.xaml
├── TicTacToe.csproj
└── UI
│ ├── Units
│ ├── GameBoard.cs
│ ├── GameBoardItem.cs
│ ├── PlayerCard.cs
│ ├── ScoreBox.cs
│ └── ScoreBoxItem.cs
│ └── Views
│ └── MainWindow.cs
└── TicTacToeWPF.sln
/.github/FUNDING.yml:
--------------------------------------------------------------------------------
1 | # These are supported funding model platforms
2 |
3 | github: [jameslee214]
4 |
--------------------------------------------------------------------------------
/.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 | [Aa][Rr][Mm]/
27 | [Aa][Rr][Mm]64/
28 | bld/
29 | [Bb]in/
30 | [Oo]bj/
31 | [Ll]og/
32 | [Ll]ogs/
33 |
34 | # Visual Studio 2015/2017 cache/options directory
35 | .vs/
36 | # Uncomment if you have tasks that create the project's static files in wwwroot
37 | #wwwroot/
38 |
39 | # Visual Studio 2017 auto generated files
40 | Generated\ Files/
41 |
42 | # MSTest test Results
43 | [Tt]est[Rr]esult*/
44 | [Bb]uild[Ll]og.*
45 |
46 | # NUnit
47 | *.VisualState.xml
48 | TestResult.xml
49 | nunit-*.xml
50 |
51 | # Build Results of an ATL Project
52 | [Dd]ebugPS/
53 | [Rr]eleasePS/
54 | dlldata.c
55 |
56 | # Benchmark Results
57 | BenchmarkDotNet.Artifacts/
58 |
59 | # .NET Core
60 | project.lock.json
61 | project.fragment.lock.json
62 | artifacts/
63 |
64 | # StyleCop
65 | StyleCopReport.xml
66 |
67 | # Files built by Visual Studio
68 | *_i.c
69 | *_p.c
70 | *_h.h
71 | *.ilk
72 | *.meta
73 | *.obj
74 | *.iobj
75 | *.pch
76 | *.pdb
77 | *.ipdb
78 | *.pgc
79 | *.pgd
80 | *.rsp
81 | *.sbr
82 | *.tlb
83 | *.tli
84 | *.tlh
85 | *.tmp
86 | *.tmp_proj
87 | *_wpftmp.csproj
88 | *.log
89 | *.vspscc
90 | *.vssscc
91 | .builds
92 | *.pidb
93 | *.svclog
94 | *.scc
95 |
96 | # Chutzpah Test files
97 | _Chutzpah*
98 |
99 | # Visual C++ cache files
100 | ipch/
101 | *.aps
102 | *.ncb
103 | *.opendb
104 | *.opensdf
105 | *.sdf
106 | *.cachefile
107 | *.VC.db
108 | *.VC.VC.opendb
109 |
110 | # Visual Studio profiler
111 | *.psess
112 | *.vsp
113 | *.vspx
114 | *.sap
115 |
116 | # Visual Studio Trace Files
117 | *.e2e
118 |
119 | # TFS 2012 Local Workspace
120 | $tf/
121 |
122 | # Guidance Automation Toolkit
123 | *.gpState
124 |
125 | # ReSharper is a .NET coding add-in
126 | _ReSharper*/
127 | *.[Rr]e[Ss]harper
128 | *.DotSettings.user
129 |
130 | # TeamCity is a build add-in
131 | _TeamCity*
132 |
133 | # DotCover is a Code Coverage Tool
134 | *.dotCover
135 |
136 | # AxoCover is a Code Coverage Tool
137 | .axoCover/*
138 | !.axoCover/settings.json
139 |
140 | # Visual Studio code coverage results
141 | *.coverage
142 | *.coveragexml
143 |
144 | # NCrunch
145 | _NCrunch_*
146 | .*crunch*.local.xml
147 | nCrunchTemp_*
148 |
149 | # MightyMoose
150 | *.mm.*
151 | AutoTest.Net/
152 |
153 | # Web workbench (sass)
154 | .sass-cache/
155 |
156 | # Installshield output folder
157 | [Ee]xpress/
158 |
159 | # DocProject is a documentation generator add-in
160 | DocProject/buildhelp/
161 | DocProject/Help/*.HxT
162 | DocProject/Help/*.HxC
163 | DocProject/Help/*.hhc
164 | DocProject/Help/*.hhk
165 | DocProject/Help/*.hhp
166 | DocProject/Help/Html2
167 | DocProject/Help/html
168 |
169 | # Click-Once directory
170 | publish/
171 |
172 | # Publish Web Output
173 | *.[Pp]ublish.xml
174 | *.azurePubxml
175 | # Note: Comment the next line if you want to checkin your web deploy settings,
176 | # but database connection strings (with potential passwords) will be unencrypted
177 | *.pubxml
178 | *.publishproj
179 |
180 | # Microsoft Azure Web App publish settings. Comment the next line if you want to
181 | # checkin your Azure Web App publish settings, but sensitive information contained
182 | # in these scripts will be unencrypted
183 | PublishScripts/
184 |
185 | # NuGet Packages
186 | *.nupkg
187 | # NuGet Symbol Packages
188 | *.snupkg
189 | # The packages folder can be ignored because of Package Restore
190 | **/[Pp]ackages/*
191 | # except build/, which is used as an MSBuild target.
192 | !**/[Pp]ackages/build/
193 | # Uncomment if necessary however generally it will be regenerated when needed
194 | #!**/[Pp]ackages/repositories.config
195 | # NuGet v3's project.json files produces more ignorable files
196 | *.nuget.props
197 | *.nuget.targets
198 |
199 | # Microsoft Azure Build Output
200 | csx/
201 | *.build.csdef
202 |
203 | # Microsoft Azure Emulator
204 | ecf/
205 | rcf/
206 |
207 | # Windows Store app package directories and files
208 | AppPackages/
209 | BundleArtifacts/
210 | Package.StoreAssociation.xml
211 | _pkginfo.txt
212 | *.appx
213 | *.appxbundle
214 | *.appxupload
215 |
216 | # Visual Studio cache files
217 | # files ending in .cache can be ignored
218 | *.[Cc]ache
219 | # but keep track of directories ending in .cache
220 | !?*.[Cc]ache/
221 |
222 | # Others
223 | ClientBin/
224 | ~$*
225 | *~
226 | *.dbmdl
227 | *.dbproj.schemaview
228 | *.jfm
229 | *.pfx
230 | *.publishsettings
231 | orleans.codegen.cs
232 |
233 | # Including strong name files can present a security risk
234 | # (https://github.com/github/gitignore/pull/2483#issue-259490424)
235 | #*.snk
236 |
237 | # Since there are multiple workflows, uncomment next line to ignore bower_components
238 | # (https://github.com/github/gitignore/pull/1529#issuecomment-104372622)
239 | #bower_components/
240 |
241 | # RIA/Silverlight projects
242 | Generated_Code/
243 |
244 | # Backup & report files from converting an old project file
245 | # to a newer Visual Studio version. Backup files are not needed,
246 | # because we have git ;-)
247 | _UpgradeReport_Files/
248 | Backup*/
249 | UpgradeLog*.XML
250 | UpgradeLog*.htm
251 | ServiceFabricBackup/
252 | *.rptproj.bak
253 |
254 | # SQL Server files
255 | *.mdf
256 | *.ldf
257 | *.ndf
258 |
259 | # Business Intelligence projects
260 | *.rdl.data
261 | *.bim.layout
262 | *.bim_*.settings
263 | *.rptproj.rsuser
264 | *- [Bb]ackup.rdl
265 | *- [Bb]ackup ([0-9]).rdl
266 | *- [Bb]ackup ([0-9][0-9]).rdl
267 |
268 | # Microsoft Fakes
269 | FakesAssemblies/
270 |
271 | # GhostDoc plugin setting file
272 | *.GhostDoc.xml
273 |
274 | # Node.js Tools for Visual Studio
275 | .ntvs_analysis.dat
276 | node_modules/
277 |
278 | # Visual Studio 6 build log
279 | *.plg
280 |
281 | # Visual Studio 6 workspace options file
282 | *.opt
283 |
284 | # Visual Studio 6 auto-generated workspace file (contains which files were open etc.)
285 | *.vbw
286 |
287 | # Visual Studio LightSwitch build output
288 | **/*.HTMLClient/GeneratedArtifacts
289 | **/*.DesktopClient/GeneratedArtifacts
290 | **/*.DesktopClient/ModelManifest.xml
291 | **/*.Server/GeneratedArtifacts
292 | **/*.Server/ModelManifest.xml
293 | _Pvt_Extensions
294 |
295 | # Paket dependency manager
296 | .paket/paket.exe
297 | paket-files/
298 |
299 | # FAKE - F# Make
300 | .fake/
301 |
302 | # CodeRush personal settings
303 | .cr/personal
304 |
305 | # Python Tools for Visual Studio (PTVS)
306 | __pycache__/
307 | *.pyc
308 |
309 | # Cake - Uncomment if you are using it
310 | # tools/**
311 | # !tools/packages.config
312 |
313 | # Tabs Studio
314 | *.tss
315 |
316 | # Telerik's JustMock configuration file
317 | *.jmconfig
318 |
319 | # BizTalk build output
320 | *.btp.cs
321 | *.btm.cs
322 | *.odx.cs
323 | *.xsd.cs
324 |
325 | # OpenCover UI analysis results
326 | OpenCover/
327 |
328 | # Azure Stream Analytics local run output
329 | ASALocalRun/
330 |
331 | # MSBuild Binary and Structured Log
332 | *.binlog
333 |
334 | # NVidia Nsight GPU debugger configuration file
335 | *.nvuser
336 |
337 | # MFractors (Xamarin productivity tool) working folder
338 | .mfractor/
339 |
340 | # Local History for Visual Studio
341 | .localhistory/
342 |
343 | # BeatPulse healthcheck temp database
344 | healthchecksdb
345 |
346 | # Backup folder for Package Reference Convert tool in Visual Studio 2017
347 | MigrationBackup/
348 |
349 | # Ionide (cross platform F# VS Code tools) working folder
350 | .ionide/
351 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | MIT License
2 |
3 | Copyright (c) 2021 DevNcore
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.
22 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | ## TicTacToe WPF
2 |
3 | 이 리포지토리는 TicTacToe를 WPF로 구현한 리포지토리입니다.
4 |
5 | 더 알아보기 »
6 |
7 | | Star | License | Activity |
8 | |:----:|:-------:|:--------:|
9 | |
|
|
|
10 |
11 |
12 |
13 | ## Contents
14 | - [개요](#개요)
15 | - [개발 환경](#개발-환경)
16 | - [학습 가이드](#학습-가이드)
17 | - [WPF 구조](#wpf-구조)
18 | - [MVVM](#mvvm)
19 |
20 |
21 |
22 | ## 개요
23 | 
24 |
25 | > 틱택토는 두 명이 번갈아가며 O와 X를 3×3 판에 써서 같은 글자를 가로, 세로, 혹은 대각선 상에 놓이도록 하는 놀이다. - [Wikipedia](https://ko.wikipedia.org/wiki/%ED%8B%B1%ED%83%9D%ED%86%A0)
26 |
27 | 틱택토는 게임 방식과 로직이 간단하여 연습 목적으로 만들어보기 좋은 게임입니다.
또한 C, Python 등 다양한 언어로 구현된 예제들이 풍부합니다.
28 |
29 | 이제 WPF로 구현한 **틱택토**를 함께 즐겨보시기 바랍니다!
30 |
31 |
32 |
33 | ## 개발 환경
34 |
35 | ✔️ **WPF .NET Core** [.NET 6.0]
36 |
37 | ✔️ **Visual Studio 2022**
38 |
39 | ✔️ **C# 10.0**
40 |
41 |
42 | 
43 |
44 |
45 |
46 | ## 학습 가이드
47 |
48 | - **숙련자:** C#과 WPF를 접해본 개발자라면 약 2시간 이내에 소스코드 전체를 작성하고 실행시킬 수 있습니다.
49 | - **초보자:** WPF와 MVVM의 이해가 부족하더라도 약 6시간 이내에 소스코드 전체를 작성하고 실행시킬 수 있습니다.
50 |
51 |
52 | **틱택토**를 통해 학습할 수 있는 기술들은 아래와 같습니다.
53 | - [x] CustomControl
54 | - [x] Trigger
55 | - [x] Mvvm 패턴
56 | - [x] DataContext
57 | - [x] RelayCommand
58 | - [x] Binding
59 | - [x] RelativeSource TemplatedParent
60 | - [x] ListBox / ListBoxItem
61 | - [x] ItemsPresenter
62 | - [x] ContentPresenter
63 | - [x] GetContainerForItemOverride
64 | - [x] OnApplyTemplate
65 | - [x] Geometry
66 | - [x] Hex Color
67 | - [x] Transparent
68 | - [x] Application
69 |
70 | WPF의 중요한 핵심 기술들을 이 앱을 통해 깊이있게 학습할 수 있습니다.
71 |
72 |
73 |
74 | ## WPF 구조
75 | 소스코드는 **`Local`** **`Themes`** **`UI`** 3개의 폴더 구조로 구성되어 있습니다. 각각의 폴더는 기능, 리소스, UI를 담당합니다.
76 |
77 |
78 |
79 | Local
80 |
81 |
82 | - Data
83 | - Mvvm
84 | - ViewModel
85 |
86 |
87 |
88 |
89 | Themes
90 |
91 |
92 | - Controls
93 |
94 |
95 |
96 |
97 | UI
98 |
99 |
100 | - Units
101 | - Views
102 |
103 |
104 | ### `Local`
105 | 로컬 기반에서 필요한 클래스 영역입니다. Model, Converter, 각종 Helper, Mvvm에 필요한 모듈, ViewModel 등 로컬에서 필요한 모든 클래스를 이 위치에서 관리합니다.
106 |
107 | ### `Themes`
108 | Generic.xaml을 포함한 리소스 분기 영역입니다. DefaultStyleKey에 해당하는 리소스를 약속된 위치(Generic.xaml)에서 다시 한번 ResourceDictionary 파일을 통해 분기하여 관리하도록 합니다.
109 |
110 | ### `UI`
111 | DeafultStyleKey를 포함하는 CustomControl 영역입니다. **`Units`** 폴더는 ListBox, ListBoxItem, Button 등과 같이 하위 요소 수준의 컨트롤 객체를 포함합니다. 그리고 **`Views`** 폴더는 Window, UserControl, ContentControl과 같이 UI 레이아웃을 담당할 수 있는 ContentPresenter 객체를 포함합니다.
112 |
113 | 
114 |
115 |
116 |
117 | ## MVVM
118 | MVVM 패턴을 사용하기 위해서는 **INotifyPropertyChanged**가 구현된 클래스가 필요합니다. 해당 앱에서는 별도의 라이브러리를 사용하지 않고 기본적으로 필요한 메서드만 로컬에 구현해서 사용합니다.
119 |
120 | ### `ObservableObject`
121 | ```csharp
122 | using System.ComponentModel;
123 | using System.Runtime.CompilerServices;
124 |
125 | namespace TicTacToe.Local.Mvvm
126 | {
127 | public class ObservableObject : INotifyPropertyChanged
128 | {
129 | public event PropertyChangedEventHandler PropertyChanged;
130 | protected void OnPropertyChanged([CallerMemberName] string name = null)
131 | {
132 | PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));
133 | }
134 | }
135 | }
136 | ```
137 |
138 |
139 |
140 | ## ScreenShot
141 | 
142 |
--------------------------------------------------------------------------------
/src/TicTacToe/App.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Windows;
3 | using TicTacToe.UI.Views;
4 |
5 | namespace TicTacToe
6 | {
7 | public class App : Application
8 | {
9 | protected override void OnStartup(StartupEventArgs e)
10 | {
11 | base.OnStartup(e);
12 | Resources.MergedDictionaries.Add(new ResourceDictionary { Source = new Uri("/DevNcore.UI.Design.Geometry;component/Themes/Packages.xaml", UriKind.RelativeOrAbsolute) });
13 | var win = new MainWindow();
14 |
15 | win.ShowDialog();
16 | }
17 | }
18 | }
19 |
--------------------------------------------------------------------------------
/src/TicTacToe/Local/Converter/EqualsToVisibilityConverter.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Globalization;
3 | using System.Windows;
4 | using System.Windows.Data;
5 | using System.Windows.Markup;
6 |
7 | namespace TicTacToe.Local.Converter
8 | {
9 | public class EqualsToVisibilityConverter : MarkupExtension, IValueConverter
10 | {
11 | public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
12 | {
13 | string selectedValue = (string)value;
14 | string currentParam = (string)parameter;
15 |
16 | return selectedValue == currentParam ? Visibility.Visible : Visibility.Collapsed;
17 | }
18 |
19 | public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
20 | {
21 | throw new NotImplementedException();
22 | }
23 |
24 | public override object ProvideValue(IServiceProvider serviceProvider)
25 | {
26 | return this;
27 | }
28 | }
29 | }
30 |
--------------------------------------------------------------------------------
/src/TicTacToe/Local/Data/Player.cs:
--------------------------------------------------------------------------------
1 | namespace TicTacToe.Local.Data
2 | {
3 | public enum Player
4 | {
5 | None,
6 | Player1,
7 | Player2
8 | }
9 | }
10 |
--------------------------------------------------------------------------------
/src/TicTacToe/Local/Data/PlayerItem.cs:
--------------------------------------------------------------------------------
1 | using System.Linq;
2 | using System.Collections.ObjectModel;
3 | using System.Collections.Specialized;
4 | using DevNcore.UI.Foundation.Mvvm;
5 |
6 | namespace TicTacToe.Local.Data
7 | {
8 | public class PlayerItem : ObservableObject
9 | {
10 | private string _playerName;
11 | private bool _isTurn;
12 | private int _winCount;
13 | private ObservableCollection _result;
14 |
15 | public string PlayerName
16 | {
17 | get => _playerName;
18 | set { _playerName = value; OnPropertyChanged(); }
19 | }
20 |
21 | public int WinCount
22 | {
23 | get => _winCount;
24 | set { _winCount = value; OnPropertyChanged(); }
25 | }
26 |
27 | public bool IsTurn
28 | {
29 | get => _isTurn;
30 | set { _isTurn = value; OnPropertyChanged(); }
31 | }
32 |
33 | public ObservableCollection Result
34 | {
35 | get => _result;
36 | set { _result = value; OnPropertyChanged(); }
37 | }
38 |
39 | public PlayerItem(string name)
40 | {
41 | PlayerName = name;
42 | Result = new();
43 | Result.CollectionChanged += Result_CollectionChanged;
44 | }
45 |
46 | private void Result_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
47 | {
48 | WinCount = Result.Where(x => x.Equals("W")).Count();
49 | }
50 |
51 | internal void SwitchTurn()
52 | {
53 | IsTurn = !IsTurn;
54 | }
55 | }
56 | }
57 |
--------------------------------------------------------------------------------
/src/TicTacToe/Local/Data/SquareItem.cs:
--------------------------------------------------------------------------------
1 | using System.Windows.Input;
2 | using DevNcore.UI.Foundation.Mvvm;
3 |
4 | namespace TicTacToe.Local.Data
5 | {
6 | public class SquareItem : ObservableObject
7 | {
8 | public ICommand ChoiceCommand { get; set; }
9 |
10 | private Player _player;
11 | public Player Player
12 | {
13 | get => _player;
14 | set { _player = value; OnPropertyChanged(); }
15 | }
16 |
17 | public string WinnerMessage => "Win: " + Player;
18 |
19 | public SquareItem(ICommand choice)
20 | {
21 | ChoiceCommand = choice;
22 | }
23 | }
24 | }
25 |
--------------------------------------------------------------------------------
/src/TicTacToe/Local/Mvvm/MainViewModel.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Windows.Input;
3 | using System.Collections.Generic;
4 | using DevNcore.UI.Foundation.Mvvm;
5 | using TicTacToe.Local.Data;
6 |
7 | namespace TicTacToe.Local.Mvvm
8 | {
9 | public class MainViewModel : ObservableObject
10 | {
11 | private int _playCount;
12 | private PlayerItem _player1;
13 | private PlayerItem _player2;
14 | private SquareItem[] _squares;
15 |
16 | public int PlayCount
17 | {
18 | get => _playCount;
19 | set { _playCount = value; OnPropertyChanged(); }
20 | }
21 |
22 | public SquareItem[] Squares
23 | {
24 | get => _squares;
25 | set { _squares = value; OnPropertyChanged(); }
26 | }
27 |
28 | public PlayerItem Player1
29 | {
30 | get => _player1;
31 | set { _player1 = value; OnPropertyChanged(); }
32 | }
33 |
34 | public PlayerItem Player2
35 | {
36 | get => _player2;
37 | set { _player2 = value; OnPropertyChanged(); }
38 | }
39 |
40 | public MainViewModel()
41 | {
42 | SetPlayers();
43 | SetSquares();
44 | }
45 |
46 | private void Choice(SquareItem square)
47 | {
48 | square.Player = (PlayCount++ % 2) switch
49 | {
50 | 0 => Player.Player1,
51 | 1 => Player.Player2,
52 | _ => Player.None
53 | };
54 |
55 | // Switch player turn.
56 | Player1.SwitchTurn();
57 | Player2.SwitchTurn();
58 |
59 | if (CalculateWinner() is SquareItem winner)
60 | {
61 | // Win
62 | Finish(winner.Player);
63 | }
64 | else if (PlayCount == 9)
65 | {
66 | // Draw
67 | Finish(Player.None);
68 | }
69 | }
70 |
71 | private void SetPlayers()
72 | {
73 | string[] players1 = { "James", "Elena", "Lucas", "Kevin" };
74 | string[] players2 = { "Harry", "Adela", "Edith", "Erica" };
75 |
76 | Player1 = new(players1[new Random().Next(0, 3)]);
77 | Player2 = new(players2[new Random().Next(0, 3)]);
78 | }
79 |
80 | private void Finish(Player player)
81 | {
82 | if (player == Player.None)
83 | {
84 | Player1.Result.Add("D");
85 | Player2.Result.Add("D");
86 | }
87 | else
88 | {
89 | Player1.Result.Add(player == Player.Player1 ? "W" : "L");
90 | Player2.Result.Add(player == Player.Player2 ? "W" : "L");
91 | }
92 | SetSquares();
93 | }
94 |
95 | private void SetSquares()
96 | {
97 | ICommand choiceCommand = new RelayCommand(Choice, (o) => o?.Player == Player.None);
98 | List data = new();
99 |
100 | for (int i = 0; i < 10; i++)
101 | {
102 | data.Add(new SquareItem(choiceCommand));
103 | }
104 | Squares = data.ToArray();
105 | PlayCount = 0;
106 | Player1.IsTurn = true;
107 | Player2.IsTurn = false;
108 | }
109 |
110 | private SquareItem CalculateWinner()
111 | {
112 | var items = Squares;
113 |
114 | List arrayList = new()
115 | {
116 | new int[] { 0, 1, 2 },
117 | new int[] { 3, 4, 5 },
118 | new int[] { 6, 7, 8 },
119 | new int[] { 0, 3, 6 },
120 | new int[] { 1, 4, 7 },
121 | new int[] { 2, 5, 8 },
122 | new int[] { 0, 4, 8 },
123 | new int[] { 2, 4, 6 }
124 | };
125 |
126 | for (int i = 0; i < arrayList.Count; i++)
127 | {
128 | var tempArray = arrayList[i];
129 |
130 | var firstType = items[tempArray[0]].Player;
131 | var secondType = items[tempArray[1]].Player;
132 | var thirdType = items[tempArray[2]].Player;
133 |
134 | if (firstType == secondType && firstType == thirdType && firstType != Player.None)
135 | {
136 | return items[tempArray[0]];
137 | }
138 | }
139 | return null;
140 | }
141 | }
142 | }
143 |
--------------------------------------------------------------------------------
/src/TicTacToe/Properties/AssemblyInfo.cs:
--------------------------------------------------------------------------------
1 | using System.Runtime.InteropServices;
2 |
3 | [assembly: System.Windows.ThemeInfo(System.Windows.ResourceDictionaryLocation.None, System.Windows.ResourceDictionaryLocation.SourceAssembly)]
4 |
5 | // In SDK-style projects such as this one, several assembly attributes that were historically
6 | // defined in this file are now automatically added during build and populated with
7 | // values defined in project properties. For details of which attributes are included
8 | // and how to customise this process see: https://aka.ms/assembly-info-properties
9 |
10 |
11 | // Setting ComVisible to false makes the types in this assembly not visible to COM
12 | // components. If you need to access a type in this assembly from COM, set the ComVisible
13 | // attribute to true on that type.
14 |
15 | [assembly: ComVisible(false)]
16 |
17 | // The following GUID is for the ID of the typelib if this project is exposed to COM.
18 |
19 | [assembly: Guid("088faa09-0e42-4a54-a285-42d2d0fc5111")]
20 |
--------------------------------------------------------------------------------
/src/TicTacToe/Starter.cs:
--------------------------------------------------------------------------------
1 | using System;
2 |
3 | namespace TicTacToe
4 | {
5 | public static class Starter
6 | {
7 | [STAThread]
8 | public static void Main(string[] args)
9 | {
10 | new App().Run();
11 | }
12 | }
13 | }
14 |
--------------------------------------------------------------------------------
/src/TicTacToe/Themes/Generic.xaml:
--------------------------------------------------------------------------------
1 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
--------------------------------------------------------------------------------
/src/TicTacToe/Themes/Units/GameBoard.xaml:
--------------------------------------------------------------------------------
1 |
4 |
5 |
27 |
--------------------------------------------------------------------------------
/src/TicTacToe/Themes/Units/GameBoardItem.xaml:
--------------------------------------------------------------------------------
1 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
40 |
41 |
50 |
--------------------------------------------------------------------------------
/src/TicTacToe/Themes/Units/PlayerCard.xaml:
--------------------------------------------------------------------------------
1 |
5 |
6 |
23 |
24 |
64 |
65 |
92 |
--------------------------------------------------------------------------------
/src/TicTacToe/Themes/Units/ScoreBox.xaml:
--------------------------------------------------------------------------------
1 |
4 |
5 |
11 |
12 |
39 |
--------------------------------------------------------------------------------
/src/TicTacToe/Themes/Units/ScoreBoxItem.xaml:
--------------------------------------------------------------------------------
1 |
6 |
7 |
25 |
--------------------------------------------------------------------------------
/src/TicTacToe/Themes/Views/MainWindow.xaml:
--------------------------------------------------------------------------------
1 |
5 |
6 |
11 |
12 |
20 |
21 |
38 |
39 |
92 |
--------------------------------------------------------------------------------
/src/TicTacToe/TicTacToe.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | WinExe
5 | net6.0-windows
6 | true
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 | $(DefaultXamlRuntime)
21 | Designer
22 |
23 |
24 | $(DefaultXamlRuntime)
25 | Designer
26 |
27 |
28 | $(DefaultXamlRuntime)
29 | Designer
30 |
31 |
32 | $(DefaultXamlRuntime)
33 | Designer
34 |
35 |
36 | $(DefaultXamlRuntime)
37 | Designer
38 |
39 |
40 | $(DefaultXamlRuntime)
41 | Designer
42 |
43 |
44 |
45 |
46 |
--------------------------------------------------------------------------------
/src/TicTacToe/UI/Units/GameBoard.cs:
--------------------------------------------------------------------------------
1 | using System.Windows;
2 | using System.Windows.Controls;
3 |
4 | namespace TicTacToe.UI.Units
5 | {
6 | public class GameBoard : ListBox
7 | {
8 | static GameBoard()
9 | {
10 | DefaultStyleKeyProperty.OverrideMetadata(typeof(GameBoard), new FrameworkPropertyMetadata(typeof(GameBoard)));
11 | }
12 |
13 | protected override DependencyObject GetContainerForItemOverride()
14 | {
15 | return new GameBoardItem();
16 | }
17 | }
18 | }
19 |
--------------------------------------------------------------------------------
/src/TicTacToe/UI/Units/GameBoardItem.cs:
--------------------------------------------------------------------------------
1 | using System.Windows;
2 | using System.Windows.Controls;
3 |
4 | namespace TicTacToe.UI.Units
5 | {
6 | public class GameBoardItem : ListBoxItem
7 | {
8 | static GameBoardItem()
9 | {
10 | DefaultStyleKeyProperty.OverrideMetadata(typeof(GameBoardItem), new FrameworkPropertyMetadata(typeof(GameBoardItem)));
11 | }
12 | }
13 | }
14 |
--------------------------------------------------------------------------------
/src/TicTacToe/UI/Units/PlayerCard.cs:
--------------------------------------------------------------------------------
1 | using System.Windows;
2 | using System.Windows.Controls;
3 |
4 | namespace TicTacToe.UI.Units
5 | {
6 | public class PlayerCard : Control
7 | {
8 | static PlayerCard()
9 | {
10 | DefaultStyleKeyProperty.OverrideMetadata(typeof(PlayerCard), new FrameworkPropertyMetadata(typeof(PlayerCard)));
11 | }
12 | }
13 | }
14 |
--------------------------------------------------------------------------------
/src/TicTacToe/UI/Units/ScoreBox.cs:
--------------------------------------------------------------------------------
1 | using System.Windows;
2 | using System.Windows.Controls;
3 |
4 | namespace TicTacToe.UI.Units
5 | {
6 | public class ScoreBox : ListBox
7 | {
8 | static ScoreBox()
9 | {
10 | DefaultStyleKeyProperty.OverrideMetadata(typeof(ScoreBox), new FrameworkPropertyMetadata(typeof(ScoreBox)));
11 | }
12 |
13 | protected override DependencyObject GetContainerForItemOverride()
14 | {
15 | return new ScoreBoxItem();
16 | }
17 | }
18 | }
19 |
--------------------------------------------------------------------------------
/src/TicTacToe/UI/Units/ScoreBoxItem.cs:
--------------------------------------------------------------------------------
1 | using System.Windows;
2 | using System.Windows.Controls;
3 |
4 | namespace TicTacToe.UI.Units
5 | {
6 | public class ScoreBoxItem : ListBoxItem
7 | {
8 | static ScoreBoxItem()
9 | {
10 | DefaultStyleKeyProperty.OverrideMetadata(typeof(ScoreBoxItem), new FrameworkPropertyMetadata(typeof(ScoreBoxItem)));
11 | }
12 | }
13 | }
14 |
--------------------------------------------------------------------------------
/src/TicTacToe/UI/Views/MainWindow.cs:
--------------------------------------------------------------------------------
1 | using System.Windows;
2 | using System.Windows.Controls;
3 | using System.Windows.Input;
4 | using TicTacToe.Local.Mvvm;
5 |
6 | namespace TicTacToe.UI.Views
7 | {
8 | public class MainWindow : Window
9 | {
10 | static MainWindow()
11 | {
12 | DefaultStyleKeyProperty.OverrideMetadata(typeof(MainWindow), new FrameworkPropertyMetadata(typeof(MainWindow)));
13 | }
14 |
15 | public MainWindow()
16 | {
17 | DataContext = new MainViewModel();
18 | WindowStyle = WindowStyle.None;
19 | AllowsTransparency = true;
20 | }
21 |
22 | public override void OnApplyTemplate()
23 | {
24 | if (GetTemplateChild("PART_Border") is Border border)
25 | {
26 | border.MouseMove += Border_MouseMove;
27 | }
28 | }
29 |
30 | private void Border_MouseMove(object sender, MouseEventArgs e)
31 | {
32 | if (e.LeftButton == MouseButtonState.Pressed && sender.Equals(e.OriginalSource))
33 | {
34 | this.DragMove();
35 | }
36 | }
37 | }
38 | }
39 |
--------------------------------------------------------------------------------
/src/TicTacToeWPF.sln:
--------------------------------------------------------------------------------
1 |
2 | Microsoft Visual Studio Solution File, Format Version 12.00
3 | # Visual Studio Version 17
4 | VisualStudioVersion = 17.0.31423.177
5 | MinimumVisualStudioVersion = 10.0.40219.1
6 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "TicTacToe", "TicTacToe\TicTacToe.csproj", "{5DA7B9EB-100B-41A2-910F-39FD879AB03E}"
7 | EndProject
8 | Global
9 | GlobalSection(SolutionConfigurationPlatforms) = preSolution
10 | Debug|Any CPU = Debug|Any CPU
11 | Release|Any CPU = Release|Any CPU
12 | EndGlobalSection
13 | GlobalSection(ProjectConfigurationPlatforms) = postSolution
14 | {5DA7B9EB-100B-41A2-910F-39FD879AB03E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15 | {5DA7B9EB-100B-41A2-910F-39FD879AB03E}.Debug|Any CPU.Build.0 = Debug|Any CPU
16 | {5DA7B9EB-100B-41A2-910F-39FD879AB03E}.Release|Any CPU.ActiveCfg = Release|Any CPU
17 | {5DA7B9EB-100B-41A2-910F-39FD879AB03E}.Release|Any CPU.Build.0 = Release|Any CPU
18 | EndGlobalSection
19 | GlobalSection(SolutionProperties) = preSolution
20 | HideSolutionNode = FALSE
21 | EndGlobalSection
22 | GlobalSection(ExtensibilityGlobals) = postSolution
23 | SolutionGuid = {BB1CBD3F-57EA-4909-9495-C80D74BDF7CC}
24 | EndGlobalSection
25 | EndGlobal
26 |
--------------------------------------------------------------------------------