質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
Python

Pythonは、コードの読みやすさが特徴的なプログラミング言語の1つです。 強い型付け、動的型付けに対応しており、後方互換性がないバージョン2系とバージョン3系が使用されています。 商用製品の開発にも無料で使用でき、OSだけでなく仮想環境にも対応。Unicodeによる文字列操作をサポートしているため、日本語処理も標準で可能です。

Q&A

解決済

1回答

404閲覧

python 三目並べ ターンを交互にする

DANNBU

総合スコア19

Python

Pythonは、コードの読みやすさが特徴的なプログラミング言語の1つです。 強い型付け、動的型付けに対応しており、後方互換性がないバージョン2系とバージョン3系が使用されています。 商用製品の開発にも無料で使用でき、OSだけでなく仮想環境にも対応。Unicodeによる文字列操作をサポートしているため、日本語処理も標準で可能です。

0グッド

0クリップ

投稿2017/07/30 14:55

###前提・実現したいこと
ここに質問したいことを詳細に書いてください
(例)PHP(CakePHP)で●●なシステムを作っています。
■■な機能を実装中に以下のエラーメッセージが発生しました。

###発生している問題・エラーメッセージ

三目並べのプログラムをpythonで作成しているのですが、サイトを参考にして 実行してみるとずっと赤い人のターンで青の人と赤の人交互にさせたいのですがどこをいじればいいのでしょうか?

###該当のソースコード

python

1from PyQt5.QtCore import (QLineF, QPointF, QRectF, Qt) 2from PyQt5.QtGui import (QBrush, QColor, QPainter) 3from PyQt5.QtWidgets import (QWidget, QApplication, QGraphicsView, QGraphicsScene, QGraphicsItem, 4 QGridLayout, QVBoxLayout, QHBoxLayout, 5 QLabel, QLineEdit, QPushButton) 6 7import numpy as np 8 9class TicTacToe(QGraphicsItem): 10 def __init__(self): 11 super(TicTacToe, self).__init__() 12 self.board = [[-1, -1, -1],[-1, -1, -1], [-1, -1, -1]] 13 self.goban = np.zeros((11, 11)) 14 self.O = 0 15 self.X = 1 16 self.turn = self.O 17 self.kuro = 1 18 self.siro = -1 19 self.space = 0 20 self.now = self.kuro 21 22 def reset(self): 23 for y in range(11): 24 for x in range(11): 25 self.board[y][x] = -1 26 self.goban[y][x] = 0 27 self.turn = self.O 28 self.update() 29 30 def select(self, x, y): 31 if x < 1 or y < 1 or x > 9 or y > 9: 32 return 33 """ 34 if self.goban[y][x] == -1: 35 self.board[y][x] = self.turn 36 self.turn = 1 - self.turn 37 38 """ 39 40 #とりあえず 41 self.goban[y][x] = self.kuro 42 43 44 def paint(self, painter, option, widget): 45 painter.setPen(Qt.black) 46 """ 47 painter.drawLine(0,100,300,100) 48 painter.drawLine(0,200,300,200) 49 painter.drawLine(100,0,100,300) 50 painter.drawLine(200,0,200,300) 51 """ 52 self.goban_left_top_x = 30 53 self.goban_left_top_y = 30 54 self.length_of_between = 30 55 self.goban_line_num = 9 56 for x in range(self.goban_left_top_x, self.goban_left_top_x + self.length_of_between * self.goban_line_num, self.length_of_between): 57 painter.drawLine(x, self.goban_left_top_y, x, self.goban_left_top_y + self.length_of_between * (self.goban_line_num - 1)) 58 59 for y in range(self.goban_left_top_y, self.goban_left_top_y + self.length_of_between * self.goban_line_num, self.length_of_between): 60 painter.drawLine(self.goban_left_top_x, y, self.goban_left_top_x + self.length_of_between * (self.goban_line_num - 1), y) 61 62 """ 63 for y in range(3): 64 for x in range(3): 65 if self.board[y][x] == self.O: 66 painter.setPen(Qt.red) 67 painter.drawEllipse(QPointF(50+x*100, 50+y*100), 30, 30) 68 elif self.board[y][x] == self.X: 69 painter.setPen(Qt.blue) 70 painter.drawLine(20+x*100, 20+y*100, 80+x*100, 80+y*100) 71 painter.drawLine(20+x*100, 80+y*100, 80+x*100, 20+y*100) 72 """ 73 for y in range(1, 10): 74 for x in range(1, 10): 75 cross_point_x = self.goban_left_top_x + self.length_of_between * (x - 1) 76 cross_point_y = self.goban_left_top_y + self.length_of_between * (y - 1) 77 if self.goban[y][x] == self.kuro: 78 painter.setPen(Qt.red) 79 painter.drawEllipse(QPointF(cross_point_x, cross_point_y), 10, 10) 80 elif self.goban[y][x] == self.siro: 81 painter.setPen(Qt.blue) 82 painter.drawEllipse(QPointF(cross_point_x, cross_point_y), 10, 10) 83 84 def boundingRect(self): 85 return QRectF(0,0,300,300) 86 87 def mousePressEvent(self, event): 88 pos = event.pos() 89 #self.select(int(pos.x()/100), int(pos.y()/100)) 90 hantei_ryouiki_satan = int(self.goban_left_top_x - int(self.length_of_between / 2)) 91 x_from_hantei_ryouiki_satan = int(pos.x()) - hantei_ryouiki_satan 92 hantei_ryouiki_joutan = int(self.goban_left_top_y - int(self.length_of_between / 2)) 93 y_from_hantei_ryouiki_joutan = int(pos.y()) - hantei_ryouiki_joutan 94 x = int(x_from_hantei_ryouiki_satan / self.length_of_between) + 1 95 y = int(y_from_hantei_ryouiki_joutan / self.length_of_between) + 1 96 print("x = {}, y = {}\n".format(x, y)) 97 self.select(x, y) 98 self.update() 99 super(TicTacToe, self).mousePressEvent(event) 100 101#class MainWindow(QGraphicsView): 102class MainWindow(QWidget): 103 def __init__(self): 104 super(MainWindow, self).__init__() 105 self.initUI() 106 107 def initUI(self): 108 self.graphicsView = QGraphicsView() 109 #scene = QGraphicsScene(self) 110 scene = QGraphicsScene(self.graphicsView) 111 scene.setSceneRect(0, 0, 400, 400) 112 self.graphicsView.setScene(scene) 113 self.tic_tac_toe = TicTacToe() 114 scene.addItem(self.tic_tac_toe) 115 116 self.kuro_button = QPushButton('黒', self) 117 self.kuro_button.setCheckable(True) 118 self.kuro_button.clicked[bool].connect(self.setColor) 119 #scene.addItem(self.kuro_button) 120 self.siro_button = QPushButton('白', self) 121 self.siro_button.setCheckable(True) 122 self.siro_button.clicked[bool].connect(self.setColor) 123 #scene.addItem(self.siro_button) 124 self.space_button = QPushButton('なし', self) 125 self.space_button.setCheckable(True) 126 self.space_button.clicked[bool].connect(self.setColor) 127 #scene.addItem(self.space_button) 128 129 vbox = QVBoxLayout() 130 vbox.addWidget(self.kuro_button) 131 vbox.addWidget(self.siro_button) 132 vbox.addWidget(self.space_button) 133 vbox.addWidget(self.graphicsView) 134 self.setLayout(vbox) 135 136 #scene.setSceneRect(0, 0, 300, 300) 137 scene.setSceneRect(0, 0, 500, 500) 138 #self.setScene(scene) 139 #self.graphicsView.setScene(scene) 140 #self.setCacheMode(QGraphicsView.CacheBackground) 141 142 mainLayout = QHBoxLayout() 143 mainLayout.addLayout(vbox) 144 145 self.setLayout(mainLayout) 146 self.setWindowTitle("Tic Tac Toe") 147 148 def keyPressEvent(self, event): 149 key = event.key() 150 if key == Qt.Key_R: 151 self.tic_tac_toe.reset() 152 super(MainWindow, self).keyPressEvent(event) 153 154 def setColor(self, pressed): 155 source = self.sender() 156 157 if source.text() == "黒": 158 self.tic_tac_toe.now = self.tic_tac_toe.kuro 159 elif source.text() == "白": 160 self.tic_tac_toe.now = self.tic_tac_toe.siro 161 elif source.text() == "なし": 162 self.tic_tac_toe.now = self.tic_tac_toe.space 163 164 165if __name__ == '__main__': 166 import sys 167 app = QApplication(sys.argv) 168 mainWindow = MainWindow() 169 170 mainWindow.show() 171 sys.exit(app.exec_())

###試したこと
def select(self, x, y):
if x < 1 or y < 1 or x > 9 or y > 9:
return
"""
if self.goban[y][x] == -1:
self.board[y][x] = self.turn
self.turn = 1 - self.turn

この部分をいじればいいのかと思い試行錯誤するも上手くいかず困っています。
###補足情報(言語/FW/ツール等のバージョンなど)
より詳細な情報

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

ベストアンサー

ちょっと改造したら動きましたよ。勝敗判定の確認などはしていませんが。


#とりあえずとしている箇所で、次のような実装を行う必要があります。

Python

1if 黒ターン: 2 ターン = 白ターン 3 self.goban[y][x] = self.kuro 4else: 5 ターン = 黒ターン 6 self.goban[y][x] = self.siro

答えを書いてしまっては面白くないので、これくらいで失礼します。

投稿2017/07/30 15:33

LouiS0616

総合スコア35660

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

DANNBU

2017/08/01 08:47

ありがとうございます
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問