(更著名的名字是三维Tic Tac趾;我只是编了一个听起来很吸引人的名字;-))
让两个人一起玩这个游戏吧。
规格:
031是这个位置:+- ++-+-+\x-+\x-+-++-+++-++-++-+ +-++-+-++-+[ws] 10基点[gr] +20点如果您有一个图形(意为非文本)网格[cc] +30点如果您可以单击一个单元格到那里- `[cp]` +20 points for building a computer player to play against
- `[ai]` +10 to 100 points for how good the AI is (judged by me)
- `[ot]` +15 points if you can choose between one and two player- `[ex]` +3 points for each interesting or confusing thing in your code that you explain in your answer (**interesting**, so don't say "n++ adds one to n" and expect to get 3 points. just be reasonable.)
- for example, you could say "I used `[a,b,c].map(max).sum%10` because it takes the foos and converts them into a bar, because {{insert explanation here}}, which I use to frob the baz."
- if you golf part of or all of your code, you will of course find many more interesting things to explain, but golfing is not required.- `[wl]` +10 points for showing the winning line, for example "000 001 002 003," or highlighting the squares in some way (if you're using a GUI)
- +5 points for every upvote (this is the [popularity-contest](/questions/tagged/popularity-contest) part)
- downvotes don't count
- include your score in the answer by using the IDs I have placed for the bonuses
- example: 10 [ws] + 20 [gr] + 20 [cp] = 50
- don't include upvotes since those change often; I will count those myself- I will accept the highest scored answer in one week; if a higher scored answer appears I will accept it
- if two answers tie, the earlier posted answer will win发布于 2013-12-03 08:17:45
10ws + 20gr + 30抄送 + 4*3例如 = 72
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.MouseEvent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.event.MouseInputListener;
public class Game {
public static void main(String[] args) {
//Initial threads
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new Game();
}
});
}
JFrame frame;
Grid grid;
Game() {
grid = new Grid();
frame = new JFrame("Tic Tac Tae Toe");
frame.add(grid);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
}
}
class Grid extends JPanel implements MouseInputListener {
static final int BEGIN_X = 50, BEGIN_Y = 50;
static final int GAP = 50;
static final int TILE_WIDTH = 40;
XO[][][] grid;
int lastX, lastY, lastZ;
int mouse_x, mouse_y;
JLabel turn;
boolean won;
public Grid() {
// Just because. I felt like copy-paste.
grid = new XO[][][]{
{
{XO.N, XO.N, XO.N, XO.N},
{XO.N, XO.N, XO.N, XO.N},
{XO.N, XO.N, XO.N, XO.N},
{XO.N, XO.N, XO.N, XO.N}
},
{
{XO.N, XO.N, XO.N, XO.N},
{XO.N, XO.N, XO.N, XO.N},
{XO.N, XO.N, XO.N, XO.N},
{XO.N, XO.N, XO.N, XO.N}
},
{
{XO.N, XO.N, XO.N, XO.N},
{XO.N, XO.N, XO.N, XO.N},
{XO.N, XO.N, XO.N, XO.N},
{XO.N, XO.N, XO.N, XO.N}
},
{
{XO.N, XO.N, XO.N, XO.N},
{XO.N, XO.N, XO.N, XO.N},
{XO.N, XO.N, XO.N, XO.N},
{XO.N, XO.N, XO.N, XO.N}
}
};
lastX = -1;
lastY = -1;
lastZ = -1;
won = false;
turn = new JLabel("X");
add(turn);
this.setPreferredSize(new Dimension(2 * BEGIN_X + 4 * TILE_WIDTH,
2 * BEGIN_Y + 4 * (4 * TILE_WIDTH + GAP) - GAP));
this.addMouseListener(this);
this.addMouseMotionListener(this);
}
public XO determineWinner() {
XO type = grid[lastZ][lastY][lastX];
//start on same z-plane
for (int x = 0; x < 4; x++) {
if (grid[lastZ][lastY][x] != type) {
break;
}
if (x == 3) {
return type; //type won!
}
}
for (int y = 0; y < 4; y++) {
if (grid[lastZ][y][lastX] != type) {
break;
}
if (y == 3) {
return type; //type won!
}
}
//first diagonal
if (lastX == lastY) {
for (int x = 0, y = 0; x < 4; x++, y++) {
if (grid[lastZ][y][x] != type) {
break;
}
if (x == 3) {
return type; //type won!
}
}
} //second diagonal
else if (lastX == 3 - lastY) {
for (int x = 0, y = 3; x < 4; x++, y--) {
if (grid[lastZ][y][x] != type) {
break;
}
if (x == 3) {
return type; //type won!
}
}
}
//first plane finished
//now onto changing z's
//vertical column
for (int z = 0; z < 4; z++) {
if (grid[z][lastY][lastX] != type) {
break;
}
if (z == 3) {
return type; //type won!
}
}
//descending y. note: descending means stair-like (diagonal)
if (lastZ == lastY) {
for (int z = 0, y = 0; z < 4; z++, y++) {
if (grid[z][y][lastX] != type) {
break;
}
if (z == 3) {
return type; //type won!
}
}
} //ascending y
else if (lastZ == 3 - lastY) {
for (int z = 0, y = 3; z < 4; z++, y--) {
if (grid[z][y][lastX] != type) {
break;
}
if (z == 3) {
return type; //type won!
}
}
} //ascending x. note: ascending means stair-like, but other way
if (lastZ == 3 - lastX) {
for (int z = 0, x = 3; z < 4; z++, x--) {
if (grid[z][lastY][x] != type) {
break;
}
if (z == 3) {
return type; //type won!
}
}
} //descending x
else if (lastZ == lastX) {
for (int z = 0, x = 0; z < 4; z++, x++) {
if (grid[z][lastY][x] != type) {
break;
}
if (z == 3) {
return type; //type won!
}
}
}
//diagonals, changing all three
if (lastZ == lastX && lastZ == lastY) {
for (int z = 0, x = 0, y = 0; z < 4; z++, x++, y++) {
if (grid[z][y][x] != type) {
break;
}
if (z == 3) {
return type; //type won!
}
}
} else if (3 - lastZ == lastX && 3 - lastZ == lastY) {
for (int z = 3, x = 0, y = 0; z < 4; z--, x++, y++) {
if (grid[z][y][x] != type) {
break;
}
if (x == 3) {
return type; //type won!
}
}
} else if (lastZ == lastX && lastZ == 3 - lastY) {
for (int z = 0, x = 0, y = 3; z < 4; z++, x++, y--) {
if (grid[z][y][x] != type) {
break;
}
if (z == 3) {
return type; //type won!
}
}
} else if (lastZ == 3 - lastX && lastZ == lastY) {
for (int z = 0, x = 3, y = 0; z < 4; z++, x--, y++) {
if (grid[z][y][x] != type) {
break;
}
if (z == 3) {
return type; //type won!
}
}
}
//check for tie
out: for (int z = 0; z < 4; z++){
for (int y = 0; y < 4; y++){
for (int x = 0; x < 4;x++){
if (grid[z][y][x] == XO.N){
break out;
}
}
}
if (z == 3){
return XO.N;
}
}
return null;
}
@Override
public void paintComponent(Graphics graphics) {
super.paintComponent(graphics);
Graphics2D g = (Graphics2D) graphics;
g.setColor(Color.BLACK);
//Looks a little nicer, but this could be left out.
g.setFont(new Font("Courier New", Font.BOLD, 20));
//paint grid rectangles.
for (int z = 0; z < 4; z++) {
int begin_y = BEGIN_Y + z * (4 * TILE_WIDTH + GAP);
//draw left line
g.drawLine(BEGIN_X, begin_y,
BEGIN_X, begin_y + 4 * TILE_WIDTH);
//draw bottom line
g.drawLine(BEGIN_X, begin_y + 4 * TILE_WIDTH,
BEGIN_X + 4 * TILE_WIDTH, begin_y + 4 * TILE_WIDTH);
for (int y = 0; y < 4; y++) {
for (int x = 0; x < 4; x++) {
//draw individual grid lines
g.drawLine(BEGIN_X + (x + 1) * TILE_WIDTH, begin_y + y * TILE_WIDTH,
BEGIN_X + (x + 1) * TILE_WIDTH, begin_y + (y + 1) * TILE_WIDTH);
g.drawLine(BEGIN_X + x * TILE_WIDTH, begin_y + y * TILE_WIDTH,
BEGIN_X + (x + 1) * TILE_WIDTH, begin_y + y * TILE_WIDTH);
//if this was the last chosen square...
if (lastX == x && lastY == y && lastZ == z) {
g.setColor(Color.ORANGE);
//fill it orange! (not required, but I think it looks nice)
g.fillRect(BEGIN_X + x * TILE_WIDTH + 1,
begin_y + y * TILE_WIDTH + 1,
TILE_WIDTH - 1, TILE_WIDTH - 1);
g.setColor(Color.BLACK);
}
//if mouse is over this square...
if ((BEGIN_X + x * TILE_WIDTH) < mouse_x
&& (BEGIN_X + (x + 1) * TILE_WIDTH) > mouse_x
&& (begin_y + y * TILE_WIDTH) < mouse_y
&& (begin_y + (y + 1) * TILE_WIDTH) > mouse_y) {
g.setColor(new Color(100, 100, 255));
//fill it blue! (not required, but I think it looks nice)
g.fillRect(BEGIN_X + x * TILE_WIDTH + 1,
begin_y + y * TILE_WIDTH + 1,
TILE_WIDTH - 1, TILE_WIDTH - 1);
g.setColor(Color.BLACK);
}
//paint value of tile
g.drawString(grid[z][y][x].val,
BEGIN_X + x * TILE_WIDTH + TILE_WIDTH / 2,
begin_y + y * TILE_WIDTH + TILE_WIDTH / 2);
}
}
}
}
@Override
public void mouseClicked(MouseEvent e) {
if (!won) {
int x = e.getX(), y = e.getY();
x -= BEGIN_X;
x /= TILE_WIDTH;
y -= BEGIN_Y;
int z = 0;
while (y > 4 * TILE_WIDTH) {
z++;
y -= 4 * TILE_WIDTH + GAP;
}
y /= TILE_WIDTH;
//**********************************
//DOES NOT ALWAYS GET PAST HERE!
//**********************************
if (x < 0 || y < 0 || z < 0 || x >= 4 || y >= 4 || z >= 4) {
return;
}
if (grid[z][y][x] == XO.N) {
grid[z][y][x] = XO.valueOf(turn.getText());
} else {
return;
}
lastX = x;
lastY = y;
lastZ = z;
XO who_won = determineWinner();
if (who_won == null){
if (turn.getText().equals("X")) {
turn.setText("O");
} else {
turn.setText("X");
}
}
else if (who_won != XO.N) {
turn.setText(who_won.toString() + " wins");
} else{
turn.setText("tie");
}
repaint();
}
}
@Override
public void mousePressed(MouseEvent e) {
}
@Override
public void mouseReleased(MouseEvent e) {
}
@Override
public void mouseEntered(MouseEvent e) {
}
@Override
public void mouseExited(MouseEvent e) {
}
@Override
public void mouseDragged(MouseEvent e) {
}
@Override
public void mouseMoved(MouseEvent e) {
mouse_x = e.getX();
mouse_y = e.getY();
repaint();
}
}
enum XO {
X("X"), O("O"), N(" ");
String val;
XO(String c) {
val = c;
}
}这是一个可行的解决方案,但我会做得更多。
例如:
JLabel的turn通知玩家该轮到谁(窗口顶部的字母将得到改进)。这个标签也用来表示谁赢了。paintComponent()期间绘制相应的方形橙色来显示最后的移动。paintComponent()期间计算鼠标指针(蓝方格)的位置截图:

发布于 2013-12-04 23:52:12
10ws+20gr+20cp+10艾城+15奥特+3例如=78
可编程和可播放从钛-84计算器。+100?
另外,->表示STO->按钮。
:Disp LOOKS-LIKE-THIS-MIGHT-BE-SOMETHING-INTERESTING-IN-THE-CODE
:ClrHome
:Disp "TIC TAC TAE TOE"
:Disp "HOW MANY PLAYERS"
:Input P
:If P=1 :Disp "YOUR OPPONENTS NAME IS AI BOB"
:If P=2 :Disp "YOUR OPPONENTS NAME IS AI BOB EVEN THOUGH YOU WANT TO PLAY TWO PLAYERS"
:Horizontal -10
:Horizontal -5
:Horizontal 0
:Horizontal 5
:Horizontal 10
:Vertical -10
:Vertical -5
:Vertical 0
:Vertical 5
:Vertical 10
:0->A
:Lbl M
:Disp "YOUR MOVE X-POS"
:Input X
:Disp "YOUR MOVE Y-POS"
:Input Y
:Circle(2,X,Y)
:Circle(2,rand,rand)
:Disp "UGH, HE DIDNT ALIGN IT CORRECTLY"
:Disp "YOU MIGHT WANT TO STOP PLAYING"
:If A=>4 :Goto W
:If A<4 :A+1->A
:Goto M
:Lbl W
:Disp "HAVE YOU WON, 1 IS YES, 0 IS NO"
:Input W
:If W=1 :Goto E
:Goto M
:Lbl E
:Disp "YOU HAVE WON! GOOD JOB!"
:Disp "ENTER TO EXIT"
:Pause有趣的是:开头的代码行(:Disp LOOKS-LIKE-THIS-MIGHT-BE-SOMETHING-INTERESTING-IN-THE-CODE)打印0,然后立即从屏幕上清除。
请给我更多的分数,我的人工智能的技能,但我很确定你会坚持10分。
https://codegolf.stackexchange.com/questions/15573
复制相似问题