This repository was archived by the owner on Apr 21, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGame.cs
More file actions
executable file
·230 lines (179 loc) · 6.26 KB
/
Game.cs
File metadata and controls
executable file
·230 lines (179 loc) · 6.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
using System;
using System.Collections.Generic;
#if ANDROID
using Android.App;
#endif
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Storage;
using Microsoft.Xna.Framework.GamerServices;
namespace Othelis
{
/// <summary>
/// This is the main type for your game
/// </summary>
public class Game1 : Microsoft.Xna.Framework.Game
{
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;
Texture2D texture, ball;
SpriteFont font;
float size, rotation;
float clippingSize = 0.0f;
Color alphaColor = Color.White;
PrimitiveBatch primitiveBatch;
OthelisEngine e = new OthelisEngine();
Tabuleiro board;
Tabuleiro temp_board;
bool wasPressed = false;
int m_player = 1;
int xBoard = -1;
int yBoard = -1;
public Game1 ()
{
graphics = new GraphicsDeviceManager (this);
Content.RootDirectory = "Content";
IsMouseVisible = true;
graphics.SupportedOrientations = DisplayOrientation.Portrait | DisplayOrientation.LandscapeLeft | DisplayOrientation.LandscapeRight | DisplayOrientation.PortraitUpsideDown;
}
/// <summary>
/// Allows the game to perform any initialization it needs to before starting to run.
/// This is where it can query for any required services and load any non-graphic
/// related content. Calling base.Initialize will enumerate through any components
/// and initialize them as well.
/// </summary>
protected override void Initialize ()
{
// TODO: Add your initialization logic here
primitiveBatch = new PrimitiveBatch(graphics.GraphicsDevice);
e.AlocaTabuleiros (out temp_board);
board = new Tabuleiro();
e.IniciaTabuleiro (board);
base.Initialize ();
}
/// <summary>
/// LoadContent will be called once per game and is the place to load
/// all of your content.
/// </summary>
protected override void LoadContent ()
{
}
/// <summary>
/// Allows the game to run logic such as updating the world,
/// checking for collisions, gathering input, and playing audio.
/// </summary>
/// <param name="gameTime">Provides a snapshot of timing values.</param>
protected override void Update (GameTime gameTime)
{
if (GamePad.GetState (PlayerIndex.One).Buttons.Back == ButtonState.Pressed) {
Exit ();
}
int h = graphics.GraphicsDevice.Viewport.Height;
int w = graphics.GraphicsDevice.Viewport.Width;
int min = Math.Min(h, w);
int x = Mouse.GetState ().X;
int y = Mouse.GetState ().Y;
int margin = min/10;
min = (min - margin)/8;
xBoard = (x - margin/2)/min;
yBoard = (y - margin/2)/min;
if (Mouse.GetState ().LeftButton == ButtonState.Pressed)
wasPressed = true;
else if (wasPressed && m_player == 1) {
if (e.PodeMoverPedra (xBoard + yBoard*8, board, m_player)) {
e.MovePedra (xBoard + yBoard*8, board, ref m_player);
}
wasPressed = false;
}
if (m_player == 2) {
int pos = -1, n1, n2, ntot, nply = 6;
int ret;
int pontos = 0;
e.ContaTotalPedras(board,out n1,out n2);/* Conta o total de peÁas no tabuleiro */
ntot = n1 + n2 + nply;
e.CopiaTabuleiro (temp_board,board);
/* Deixa o computador,verificar melhor jogada */
ret = e.EncontreMelhorPosicao (temp_board, nply, m_player, ref pontos, ref pos,0, -OthelisEngine.OTH_INFINITO, OthelisEngine.OTH_INFINITO);
if(ret != -1 && pos >= 0 && pos < 64)
{
e.MovePedra(pos,board,ref m_player);
}
}
base.Update (gameTime);
}
void DrawLine(Vector2 start, Vector2 end, Color c)
{
primitiveBatch.Begin(PrimitiveType.LineList);
// from the nose, down the left hand side
primitiveBatch.AddVertex(start, c);
primitiveBatch.AddVertex(end, c);
primitiveBatch.End();
}
void DrawRect (Vector2 topLeft, Vector2 size, Color c)
{
primitiveBatch.Begin(PrimitiveType.TriangleList);
// from the nose, down the left hand side
primitiveBatch.AddVertex(topLeft, c);
primitiveBatch.AddVertex(new Vector2(topLeft.X + size.X, topLeft.Y), c);
primitiveBatch.AddVertex(new Vector2(topLeft.X, topLeft.Y + size.Y), c);
primitiveBatch.AddVertex(new Vector2(topLeft.X + size.X, topLeft.Y), c);
primitiveBatch.AddVertex(new Vector2(topLeft.X + size.X, topLeft.Y + size.Y), c);
primitiveBatch.AddVertex(new Vector2(topLeft.X, topLeft.Y + size.Y), c);
primitiveBatch.End();
}
void DrawBoard (Vector2 start, Vector2 size)
{
DrawWireBoard (start, size);
DrawStones (start, size);
}
void DrawWireBoard (Vector2 start, Vector2 size)
{
int min_size = (int)Math.Min (size.X, size.Y);
int step = min_size / 8;
for (int i = 0; i <= min_size; i += step) {
DrawLine (new Vector2 (start.X, start.Y + i), new Vector2 (start.X + min_size, start.Y + i), Color.LightGreen);
DrawLine (new Vector2 (start.X + i, start.Y), new Vector2 (start.X + i, start.Y + min_size), Color.LightGreen);
}
}
void DrawStones (Vector2 start, Vector2 size)
{
int min_size = (int)Math.Min (size.X, size.Y);
int step = min_size / 8;
for(int y = 0; y < 8; y++)
{
for(int x = 0; x < 8; x++)
{
if (x == xBoard && y == yBoard) {
Color color = e.PodeMoverPedra (xBoard + 8*yBoard, board, 1) ? Color.Yellow : Color.YellowGreen;
DrawRect (new Vector2 (start.X + x*step, start.Y + y*step), new Vector2 (step, step), color);
}
if(e.get(board.p1, x + y*8) != 0)
{
DrawRect (new Vector2 (start.X + x*step, start.Y + y*step), new Vector2 (step, step), Color.Red);
}
if(e.get(board.p2, x + y*8) != 0)
{
DrawRect (new Vector2 (start.X + x*step, start.Y + y*step), new Vector2 (step, step), Color.Blue);
}
}
}
}
/// <summary>
/// This is called when the game should draw itself.
/// </summary>
/// <param name="gameTime">Provides a snapshot of timing values.</param>
protected override void Draw (GameTime gameTime)
{
graphics.GraphicsDevice.Clear (Color.Black);
int h = graphics.GraphicsDevice.Viewport.Height;
int w = graphics.GraphicsDevice.Viewport.Width;
int min = Math.Min(h, w);
int margin = min/10;
min = min - margin;
DrawBoard (new Vector2(margin/2, margin/2), new Vector2 (min, min));
base.Draw (gameTime);
}
}
}