HasWon
How do I use Has Won
Below are practical examples compiled from projects for learning and reference purposes
Featured Snippets
File name: Board.cs
Copy
69 public bool HasWon(Seed seed)
70 {
71 if (lastChangedCell == null)
72 {
73 return false;
74 }
75
76 return HasWonInTheRow(seed)
77 || HasWonInTheColumn(seed)
78 || HasWonInTheDiagonal(seed)
79 || HasWonInTheOppositeDiagonal(seed);
80 }
File name: Board.cs
Copy
82 private bool HasWonInTheRow(Seed seed)
83 {
84 int row = lastChangedCell.Row;
85
86 return cells[row, 0].HasSeed(seed)
87 && cells[row, 1].HasSeed(seed)
88 && cells[row, 2].HasSeed(seed);
89 }
File name: Board.cs
Copy
91 private bool HasWonInTheColumn(Seed seed)
92 {
93 int col = lastChangedCell.Col;
94
95 return cells[0, col].HasSeed(seed)
96 && cells[1, col].HasSeed(seed)
97 && cells[2, col].HasSeed(seed);
98 }
File name: Board.cs
Copy
100 private bool HasWonInTheDiagonal(Seed seed)
101 {
102 return cells[0, 0].HasSeed(seed)
103 && cells[1, 1].HasSeed(seed)
104 && cells[2, 2].HasSeed(seed);
105 }
File name: Board.cs
Copy
107 private bool HasWonInTheOppositeDiagonal(Seed seed)
108 {
109 return cells[0, 2].HasSeed(seed)
110 && cells[1, 1].HasSeed(seed)
111 && cells[2, 0].HasSeed(seed);
112 }
File name: Game.cs
Copy
169 private void OnBoardChange(Seed player, int row, int col)
170 {
171 Seed nextPlayer = Seed.Empty;
172
173 if (board.HasWon(player))
174 {
175 switch (player)
176 {
177 case Seed.Cross:
178 CurrentState = GameState.CrossWin;
179 Player1.Score++;
180 break;
181 case Seed.Nought:
182 CurrentState = GameState.NoughtWin;
183 Player2.Score++;
184 break;
185 }
186
187 OnGameResultSignal.Dispatch(this);
188 }
189 else if (board.IsDraw())
190 {
191 CurrentState = GameState.Draw;
192 OnGameResultSignal.Dispatch(this);
193 }
194 else
195 {
196 nextPlayer = player == Seed.Cross ? Seed.Nought : Seed.Cross;
197 }
198
199 board.SetPlayer(nextPlayer);
200
201 if (NetworkService.IsConnected && player == Player1.Type)
202 {
203 board.SetPlayer(Seed.Empty);
204 NetworkService.SendBoardChange(player, row, col);
205 }
206 }
HasWon 132 lượt xem
Gõ tìm kiếm nhanh...